Sending Embedded Messages

Messages can be attached to other messages. You may have seen an example of this when a mail server sends you a non-delivery report and embeds a copy of your original email.

To attach a message to an outgoing message, in the call to the Attachments collection’s Add method, set the Type parameter to CdoEmbeddedMessage. Set the Source parameter to the message ID of the message to be embedded. The message ID can be retrieved from the ID property of a Message object. The value assigned to the Add method’s Name parameter will be used by the receiving system to label the representation of the embedded message in the UI. A good choice for this parameter is the subject of the embedded message. The Position parameter has its usual meaning.

The code looks like this:

' (CdoMessage and CdoMessage2 previously Dim'ed and Set)
   
Dim CdoAttachments As MAPI.Attachments
Dim CdoAttachment As MAPI.Attachment
   
CdoMessage.Subject = "Embedding a Message"
CdoMessage.Text = "There should be a message here:" _
   & vbCr & vbCr
   
Set CdoAttachments = CdoMessage.Attachments
Set CdoAttachment = CdoAttachments.Add( _
   Name:=CdoMessage2.Subject, _
   Position:=Len(CdoMessage.Text) + 1, _
   Type:=CdoEmbeddedMessage, Source:=CdoMessage2.ID)

Figure 6-5 shows the received message, as displayed by Microsoft Outlook.

A message with an embedded message

Figure 6-5. A message with an embedded message

Get CDO & MAPI Programming with Visual Basic: now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.