Microsoft KB Archive/318944

= XADM: A Replied or Forwarded CDO.Message Appears Rearranged on the Second Iteration of the Reply =

Article ID: 318944

Article Last Modified on 10/26/2006

-

APPLIES TO


 * Microsoft Exchange 2000 Server Standard Edition
 * Microsoft Exchange 2000 Enterprise Server

-



This article was previously published under Q318944



SYMPTOMS
If you (as a developer) modify the HTMLBody property of a reply to a CDO.Message, the message may appear rearranged on the second iteration of the reply.



CAUSE
This issue can occur if you did not modify the HTMLBody property of the reply to the CDO.Message correctly. When you modify the HTMLBody property of a reply to a CDO.Message, you must make sure that you create properly formatted Hypertext Markup Language (HTML) and insert it correctly into the body of the message.

Although the message appears rearranged, the message is actually still formatted just as you created it.

NOTE: The following code is simply an example to reproduce this issue. However, the situation that this example describes is possible, and has been encountered with third-party developers. This article describes what occurs in this situation and provides a method that you can use to correct your code.

For example, consider the following Microsoft Visual Basic code:

Public Sub Main Dim MsgName Dim szPrefix Dim Original As CDO.Message Dim Reply As CDO.Message Dim strReplyText1 As String Dim strReplyText2 As String

strReplyText1 = &quot;This is REPLY HTML 1 &quot; strReplyText2 = &quot;This is REPLY HTML 2 &quot;

MsgName = &quot;HTMLMessage.EML&quot;

szPrefix = &quot;&quot; Set Original = New CDO.Message Original.DataSource.Open &quot;file://./backofficestorage/cpr-ex-lc.dom/mbx/charliea/inbox/&quot; & szPrefix & MsgName Set Reply = Original.Reply Reply.HTMLBody = strReplyText1 + Reply.HTMLBody Reply.From = &quot;Charlie Andersen&quot; Reply.Send Set Reply = Nothing Set Original = Nothing

szPrefix = &quot;RE%3A &quot; Set Original = New CDO.Message Original.DataSource.Open &quot;file://./backofficestorage/cpr-ex-lc.dom/mbx/charliea/inbox/&quot; & szPrefix & MsgName Set Reply = Original.Reply Reply.HTMLBody = strReplyText2 + Reply.HTMLBody Reply.From = &quot;Charlie Andersen&quot; Reply.Send Set Reply = Nothing Set Original = Nothing

End Sub

Synopsis
During the INITIAL reply, Collaboration Data Objects (CDO) parses the following original message:

Received: from SMTPStorm ([157.57.146.15]) by tigger-530.cpr-ex-lc.dom with Microsoft SMTPSVC(5.0.2195.4453); Thu, 14 Feb 2002 09:36:19 -0600 From: Charlie Andersen To: Charlie Andersen Subject: HTMLMessage MIME-Version: 1.0 Content-Type: text/html; charset=&quot;iso-8859-1&quot; Content-Transfer-Encoding: 7bit

Type message Here

CDO reformats the original message with the correct reply headers in the body of the message as follows:

 -Original Message- From:   "Charlie Andersen" <charliea@cpr-ex-lc.dom>  Sent:</B>   14 February, 2002 09:36  To:</B> "Charlie Andersen" <charliea@cpr-ex-lc.dom>  Subject:</B> HTMLMessage   Type message Here

At this point, the issue begins to compound. When you use the following syntax, you inadvertently create malformed HTML by placing HTML tags and text outside the  tags of the HTML stream:

<pre class="fixed_text">Reply.HTMLBody = strReplyText1 + Reply.HTMLBody

You create a message with a body that is similar to the following:

<pre class="fixed_text"> This is REPLY HTML 1 -Original Message- From:</B>   "Charlie Andersen" <charliea@cpr-ex-lc.dom>  Sent:</B>   14 February, 2002 09:36  To:</B> "Charlie Andersen" <charliea@cpr-ex-lc.dom> <BR> <B>Subject:</B> HTMLMessage <BR> <BR> Type message Here

This in itself renders correctly in HTML view (in Microsoft Internet Explorer) because there are no items that should be viewed above the reply or forward header information. Internet Explorer is a very forgiving browser for malformed HTML. All of the Microsoft Outlook and Microsoft Outlook Express windows that display HTML use some form of the Internet Explorer HTML parsing and viewing mechanism.

When a user replies to the second message, the issues that are created by your (the third-party developer) code are made evident; this code opens the reply to the first message, and then attempts to reply to that message with another message.

CDO correctly adds all of the correct reply or forwarding information to the HTMLBody property and CDO correctly keeps all of that information inside the <BODY> tags of the message body.

CDO does not place the message text from the first reply in the <BODY> tag of the new message body because that message text was not found in the <BODY> tag to begin with (because of the programming error in the first reply). Therefore, when CDO commits the reply, the message is similar to the following:

<pre class="fixed_text"> This is REPLY TEXT 1 <BR> -Original Message-<BR> <B>From:</B>   "Charlie Andersen" <charliea@cpr-ex-lc.dom> <BR> <B>Sent:</B>   22 February, 2002 14:43 <BR> <B>To:</B> "Charlie Andersen" <charliea@cpr-ex-lc.dom> <BR> <B>Subject:</B> RE: HTMLMessage <BR> <BR> <BR> -Original Message-<BR> <B>From:</B>   "Charlie Andersen" <charliea@cpr-ex-lc.dom> <BR> <B>Sent:</B>   14 February, 2002 09:36 <BR> <B>To:</B> "Charlie Andersen" <charliea@cpr-ex-lc.dom> <BR> <B>Subject:</B> HTMLMessage <BR> <BR> Type message Here

When you (the developer) run the following code, &quot; This is REPLY HTML 2 &quot; (without the quotation marks) is inserted immediately preceding the text from the first reply:

<pre class="fixed_text">Reply.HTMLBody = strReplyText2 + Reply.HTMLBody

Therefore, the message is displayed as follows:

<pre class="fixed_text"> This is REPLY HTML 2 This is REPLY TEXT 1 <BR> -Original Message-<BR> <B>From:</B>   "Charlie Andersen" = <charliea@cpr-ex-lc.dom> <BR> <B>Sent:</B>   22 February, 2002 14:43 <BR> <B>To:</B> "Charlie Andersen" <charliea@cpr-ex-lc.dom> <BR> <B>Subject:</B> RE: HTMLMessage <BR> <BR> <BR> -Original Message-<BR> <B>From:</B>   "Charlie Andersen" <charliea@cpr-ex-lc.dom> <BR> <B>Sent:</B>   14 February, 2002 09:36 <BR> <B>To:</B> "Charlie Andersen" <charliea@cpr-ex-lc.dom> <BR> <B>Subject:</B> HTMLMessage <BR> <BR> Type message Here

This renders the two lines of successive reply before any of the reply or forwarding information, and is nothing like what you intended.

<div class="resolution_section">

RESOLUTION
To resolve this issue in your code, review the following code, which demonstrates how to correctly insert new HTML and text into an HTML-formatted message by using CDO:

<pre class="fixed_text"> ' Function to split the BODY of the HTML into two parts, immediately after the end of the BODY tag. ' That way, the new message text is prepended ABOVE the prior message, but AFTER the body tag is complete. ' Public Sub SplitBody(strHTML As String, strFirstPart As String, strLastPart As String) Dim lngPtr As Long Dim lngBodyBegin As Long Dim lngBodyEnd As Long Dim lngI As Long Dim lngLength As Long ' find <BODY in stream lngBodyBegin = InStr(1, UCase$(strHTML), &quot;<BODY&quot;, vbTextCompare) ' did we find one? If lngBodyBegin > 0 Then ' Find the <BODY terminator lngBodyEnd = InStr(lngBodyBegin, strHTML, &quot;>&quot;, vbTextCompare) ' did we find one? If lngBodyEnd > 0 Then strFirstPart = Left$(strHTML, lngBodyEnd) strLastPart = Mid$(strHTML, lngBodyEnd + 1) End If   End If End Sub

' A function that was written to provide a resolution to this issue. ' Insert a string of text after the body tag. ' Sub InjectIntoBody(Msg As CDO.Message, HtmlText As String) Dim strHeader As String Dim strTrailer As String SplitBody Msg.HTMLBody, strHeader, strTrailer Msg.HTMLBody = strHeader + HtmlText + strTrailer End Sub

Public Sub Main Dim MsgName Dim szPrefix Dim Original As CDO.Message Dim Reply As CDO.Message Dim strReplyText1 As String Dim strReplyText2 As String

strReplyText1 = &quot;<div style=&quot;&quot;{font-Weight:700;}&quot;&quot;>This is REPLY HTML 1 &quot; strReplyText2 = &quot;<div style=&quot;&quot;{font-Weight:700;}&quot;&quot;>This is REPLY HTML 2 &quot;

MsgName = &quot;HTMLMessage.EML&quot;

szPrefix = &quot;&quot; Set Original = New CDO.Message Original.DataSource.Open &quot;file://./backofficestorage/cpr-ex-lc.dom/mbx/charliea/inbox/&quot; & szPrefix & MsgName Set Reply = Original.Reply InjectIntoBody Reply, strReplyText1 Reply.From = &quot;Charlie Andersen<charliea@cpr-ex-lc.dom>&quot; Reply.Send Set Reply = Nothing Set Original = Nothing

szPrefix = &quot;RE%3A &quot; Set Original = New CDO.Message Original.DataSource.Open &quot;file://./backofficestorage/cpr-ex-lc.dom/mbx/charliea/inbox/&quot; & szPrefix & MsgName Set Reply = Original.Reply InjectIntoBody Reply, strReplyText2 Reply.From = &quot;Charlie Andersen<charliea@cpr-ex-lc.dom>&quot; Reply.Send Set Reply = Nothing Set Original = Nothing

End Sub

In this example, CDO parses the HTML Message body to determine where the actual HTML body begins and inserts the new message text and HTML at that point.

Additional query words: Exchange CDO Programming Tip

Keywords: kbarttypeinf kbmsg kbprb KB318944

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.