HOWTO: Send an HTML Message Using MAPI and CDO

ID: Q216344


The information in this article applies to:
  • Extended Messaging Application Programming Interface (MAPI), version 1.0
  • Collaboration Data Objects (CDO), version 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.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 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 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

   ' <TO DO:> 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.


REFERENCES

For additional information about creating and storing message text to an RTF stream, please see the following article in the Microsoft Knowledge Base:

Q172038 FILE: DLL to Read and Write RTF with Active Messaging
For a sample Visual Basic application that demonstrates the Rich Text Format, please see the following article in the Microsoft Knowledge Base:
Q216346 SAMPLE: RTFView.exe Rich Text Format Stream Viewer

Additional query words: kbCDO121 kbMsg kbMAPI100

Keywords : kbCDO121 kbMsg kbMAPI100 kbfaq
Version : WINDOWS:1.0,1.21,2000,98
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: January 25, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.