Microsoft KB Archive/216344

{|
 * width="100%"|

HOWTO: Send an HTML Message Using MAPI and CDO

 * }

-

The information in this article applies to:


 * Extended Messaging Application Programming Interface (MAPI), version 1.0
 * Collaboration Data Objects (CDO), versions 1.2, 1.21
 * Microsoft Outlook, versions 2000, 98

-

SUMMARY
Microsoft Outlook has the ability to compose, receive, and render e-mail messages with bodies of different format types. Such formats include Plain Text, Rich Text Format (RTF), and Hypertext Markup Language (HTML). Outlook uses a Rich Text Edit control to display the contents of the message body. The text control interprets various tags in the message body stream to determine in which format the message body should be displayed. This article shows how to force Outlook's Rich Text Edit control to display the message content in HTML, programmatically.

NOTE: This information is specific to and will only work with Microsoft Outlook 98 and later and CDO 1.2 or 1.21. No other e-mail client is supported nor guaranteed to display HTML or RTF correctly.

MORE INFORMATION
Composing an HTML-based message is very similar to composing a message in Rich Text Format. If you can compose an RTF message, you can compose an HTML message. You only need to modify the RTF stream to indicate that the message is from HTML instead of Plain Text or RTF. Making a simple modification to the RTF stream communicates to Outlook's RTF edit control which display format to use.

CDO (1.x) does not natively support RTF or HTML. The only supported mechanism for accessing and working with RTF in an e-mail message is the Messaging Application Programming Interface (MAPI). MAPI code can only be written in C or C++. Therefore, you must write any code that works with RTF in C or C++. The first Microsoft Knowledge Base article listed in the REFERENCES section discusses accessing and manipulating RTF streams in full detail and provides an easy to use C++ library for doing so.

C or C++ programmers can use the C++ code in this article as a guideline for writing their own RTF support code or use the library (LIB) file as is. Visual Basic programmers can use the included Dynamic Link Library (DLL) to provide RTF support.

The body of an Outlook e-mail message is stored in an RTF stream. The stream is made up of the plain text of the message as well as several tags that tell Outlook how to render the message. The following tags are used to tell Outlook which format type to use to render the message body:


 * HTML -> \fromhtml1
 * Plain Text -> \fromtext

Forcing the contents of your message to be displayed in HTML in Outlook requires you to wrap your HTML text in an RTF stream and then add the \fromhtml1 tag to the beginning of the stream. In a Microsoft Visual Basic program, the code would look very similar to the following:

  Dim strRTFStream as String

'  Add code to obtain RTF Stream pointer. ' Microsoft Knowledge base article Q172038 provides a code sample. ' Set strRTFStream equal to results from sample code.

' Pre-pend strRTFStream with HTML tag strRTFStream = "{\fromhtml1" & vbCrLf & strRTFStream & "}" Sample code that demonstrates this method in C and C++ is intentionally left out of this article. However, the concept remains the same. Simply place the \fromhtml1 tag at the beginning of the RTF Stream. The RTF edit control will do the rest.