HOWTO: Send Rich Text Formatted E-Mail from Visual Fox Pro Using CDO

ID: Q231574


The information in this article applies to:


SUMMARY

This article describes how to use Microsoft's Collaboration Data Objects (CDO) library from Visual FoxPro to send an e-mail message with a rich text formatted (RTF) message body.


MORE INFORMATION

The following Visual FoxPro code sample sends an e-mail message with an RTF formatted message body.

Notes

This code sample assumes that CDO 1.1 or greater has been installed on the target system. If you're not certain that this library is already installed please see the following article in the Microsoft Knowledge Base:
Q171440 Where to Acquire the Collaboration Data Objects Libraries
This article also makes use of a function (writertf) provided in a DLL (Mapirtf.dll) delivered in a second article:
Q172038 DLL to Read and Write RTF with Active Messaging
(CDO was formerly named Active Messaging).

The code below will not function until both the CDO library and the Mapirtf.dll are installed as noted in their respective readme files.

* Send RTF E-Mail from VFP
* -----------------------
* Put Mapirtf.dll in the current directory, or Windows\System directory
* (for Windows 95) or the Winnt\System32 directory (for Windows NT).

Declare INTEGER writertf IN mapirtf.dll;
       STRING ProfileName, STRING MessageID,;
       STRING StoreID,     STRING cText

Declare INTEGER readrtf IN mapirtf.dll;
       STRING ProfileName, STRING SrcMsgID,;
       STRING SrcStoreID,  STRING MsgRTF
       
objSession = CREATEOBJECT("mapi.session")
objSession.Logon

*Create a new message in the Outbox
objMessage = objSession.Outbox.Messages.Add

*Set the Subject and Add a Recipient
objMessage.Subject = "This is the message subject"
objRecip = objMessage.Recipients.Add
objRecip.name = "My_Recip_Here"  && <<--Your Recipients email name here
objRecip.Resolve

*Write the message to the store in order to acquire then
*retrieve a MessageID
objMessage.Update
MsgID = objMessage.ID

*Write the RTF, passing the saved MessageID, StoreID, and the
*RTF Stream to the function.
*
*Note - The RichEdit Control that ships with VF does not expose the
*       RTF Stream - just plain text. You will need to either use an
*       alternate RTF Control that exposes the RTF Stream, or read
*       the RTF from a file, etc. The following line of code loads
*       an RTF Stream from file into the vRTFStream variable.
*
*For testing purposes, try reading the contents of a file saved
*in an RTF format from MS-Word into the variable, alternately you
*will need to find an RTF control that exposes the RTF stream as
*a property of the control:
vRTFStream = FILETOSTR(GETFILE("RTF",;
                               "",;
                               "Read this File",;
                               0,;
                               "Select an RTF Source File"))

*The writertf() function is in the MAPIRTF.DLL from Q172038
intRetVal = writertf(objSession.Name,;
                     objMessage.ID,;
                     objMessage.StoreID,;
                     vRTFStream)
            
IF intRetVal <> 0 THEN
       MESSAGEBOX("Write RTF to Message >> Failed <<")
ELSE
       MESSAGEBOX("Write RTF to Message >> Succeeded <<")
ENDIF

*Because the object has changed, we must re-acquire our Message...
*First clear our current variable
RELEASE objMessage

*Next set a filter on the outbox for our Message ID
objMessageColl = objSession.Outbox.Messages
objMessageFilter = objMessageColl.Filter
objMessageFilter.Fields.Add(0x0FFF0102, MsgID)
objMessage = objMessageColl.GetFirst

*Send it
objMessage.Send

*Clean up then exit
objSession.Logoff
RELEASE objRecip, objMessage, objSession
*EOP: VF_CDO_RTF.PRG 


REFERENCES

For additional information on how to effectively use CDO, see the Microsoft Knowledge Base articles and the Microsoft Developer Network (MSDN). Both are great resources, containing a substantial number of additional articles on how to make use of CDO. To find these articles, query on "CDO" or "Active Messaging."

Additional query words: kbActMsg kbCDO kbCDO110 kbCDO120 kbCDO121 kbMsg kbGrpMsg kbDSupport kbVFp


Keywords          : kbActMsg kbCDO kbCDO110 kbCDO120 kbCDO121 kbMsg kbVFp kbGrpMsg kbDSupport 
Version           : WINDOWS:1.1,1.2,1.21
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: June 9, 1999