Manually Adding Attachments
Topic Last Modified: 2006-06-12
You can manually add attachments directly to the IMessage.Attachments or IBodyPart.BodyParts collection that is exposed by the Message object. This approach is useful if you have content that is not on the file system or URL-addressable. When manually adding an attachment, use the IMessage.Attachments collection as described in the following procedure.
To manually add an attachment using the IMessage.Attachments collection
- Create a new BodyPart object in the Attachments collection using IBodyParts.Add. (The Attachments property on the object returns a BodyParts collection object reference.)
- Set appropriate Multipurpose Internet Mail Extensions (MIME) header fields for the body part.
- Obtain the content stream for the attachment using IBodyPart.GetDecodedContentStream and write the content to it.
- Call _Stream.Flush to commit written data to the body part.
Example
Visual Basic
Dim iMsg As New CDO.Message
Dim iBp As CDO.IBodyPart
Dim Stm As ADODB.Stream
Dim Flds As ADODB.Fields
With iMsg
.To = "someone@example.com"
.From = "another@example.com"
.Newsgroups = "comp.example.newsgroup1"
.Subject = "Agenda for staff meeting"
.TextBody = "Please plan to present your status for the following projects..."
Set iBp = .Attachments.Add
Set Flds = iBp.Fields
With Flds
.Item("urn:schemas:mailheader:content-type") = "text/plain; name=test.txt"
.Item("urn:schemas:mailheader:content-transfer-encoding") = "quoted-printable"
.Update
End With ' Flds
Set Flds = Nothing
Set Stm = iBp.GetDecodedContentStream
' Because body part content-type is "text", the returned Stream
' is of type adTypeText. Use WriteText to fill the stream
Stm.WriteText "Here is the text in the ""attachment"" called text.txt"
' commit the changes into the BodyPart object
Stm.Flush
Set Stm = Nothing
End With ' iMsg
C++, IDL
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace
#import "c:\program files\common files\microsoft shared\cdo\cdoex.dll" no_namespace
// ...
IMessagePtr iMsg(__uuidof(Message));
IBodyPartPtr iBp;
_StreamPtr Stm;
FieldsPtr Flds;
iMsg->To = "someone@example.com";
iMsg->From = "another@example.com";
iMsg->Subject = "Agenda for staff meeting";
iMsg->TextBody = "Please plan to present your status for the following
projects...";
iBp = iMsg->Attachments->Add(-1);
Flds = iBp->Fields;
Flds->Item["urn:schemas:mailheader:content-type"]->Value
= _variant_t("text/plain; name=\"test.txt\"");
Flds->Update();
Stm = iBp.GetDecodedContentStream();
/*
** Because body part content-type is "text", the returned Stream
** is of type adTypeText. Use WriteText to fill the stream
*/
Stm->WriteText("Here is the text in the ""attachment"" called
text.txt",adWriteChar);
' commit the changes into the BodyPart object
Stm->Flush();
// ...
if(g_Debug)
cout << iMsg->GetStream()->ReadText(-1);
VBScript
Dim iMsg
Set iMsg = CreateObject("CDO.Message")
Dim iBp
Dim Stm
Dim Flds
With iMsg
.To = "someone@example.com"
.From = "another@example.com"
.Subject = "Agenda for staff meeting"
.TextBody = "Please plan to present your status for the following
projects..."
Set iBp = .Attachments.Add
Set Flds = iBp.Fields
With Flds
.Item("urn:schemas:mailheader:content-type") =
"text/plain; name=test.txt"
.Item("urn:schemas:mailheader:content-transfer-encoding") = "quoted
printable"
.Update
End With ' Flds
Set Flds = Nothing
Set Stm = iBp.GetDecodedContentStream
' Because body part content-type is "text", the returned Stream
' is of type adTypeText. Use WriteText to fill the stream
Stm.WriteText "Here is the text in the ""attachment"" called text.txt"
' commit the changes into the BodyPart object
Stm.Flush
Set Stm = Nothing
End With ' iMsg
You can add any type of attachment to a message. For MIME-formatted messages, attachments are added as MIME body parts. Collaboration Data Objects (CDO) automatically sets the MIME Content-Type and Content-Transfer-Encoding values for some attachments, but for some attachment types you need to set these values explicitly.
If you want to have the message sent with attachments formatted and encoded with Unix-to-Unix encode (UUENCODE), set the IMessage.MIMEFormatted property to False before sending the message.
In many cases, you might need to specify credentials to access a resource on the network. Add this information to the Message object's associated Configuration object. See Configuring the Message Object for more information.
See Also
Other Resources
IMessage.CreateMHTMLBody Method
IMessage.Attachments Property
Creating MHTML-Formatted Messages