Platform SDK: CDO for Windows 2000 |
The following example shows how to manually create a body part hierarchy for a message.
Dim iMsg As New CDO.Message Dim iBp As CDO.IBodyPart Dim iBp1 As CDO.IBodyPart Dim iBp2 As CDO.IBodyPart Dim Flds As ADODB.Fields Dim Stm As ADODB.Stream Set iBp = iMsg ' get IBodyPart on Message object Set Flds = iBp.Fields Flds("urn:schemas:mailheader:content-type") = "multipart/mixed" Flds.Update Set iBp1 = iBp.AddBodyPart ' Add a BodyPart object to the hierarchy Set iBp2 = iBp1.AddBodyPart ' text/plain Set Flds = iBp2.Fields Flds("urn:schemas:mailheader:content-type") = "text/plain" Flds("urn:schemas:mailheader:content-transfer-encoding") = "quoted-printable" Flds.Update Set Stm = iBp2.GetDecodedContentStream Stm.WriteText "This is a message" Stm.Flush Set iBp2 = iBp1.AddBodyPart ' text/html Set Flds = iBp2.Fields Flds("urn:schemas:mailheader:content-type") = "text/html" Flds("urn:schemas:mailheader:content-transfer-encoding") = "quoted-printable" Flds.Update Set Stm = iBp2.GetDecodedContentStream Stm.WriteText "<html><h1>This is a message</h1></html>" Stm.Flush ' This may appear out of order, but we have to _reset_ the ' content-type for the "multipart/alternative" body part after adding ' the two representations. If we set it before, it will be switched ' to "multipart/mixed" when we call the AddBodyPart() method on the ' object. Set Flds = iBp1.Fields Flds("urn:schemas:mailheader:content-type") = "multipart/alternative" Flds.Update Set iBp1 = iBp.AddBodyPart ' image/gif Set Flds = iBp1.Fields Flds("urn:schemas:mailheader:content-type") = "image/gif" Flds("urn:schemas:mailheader:content-transfer-encoding") = "base64" Set Stm = iBp1.GetDecodedContentStream Stm.LoadFromFile "c:\images\myimage.gif" Strm.Flush
#import "d:\program files\common files\system\ado\msado15.dll" no_namespace #import <cdosys.dll> no_namespace void main() { CoInitialize(NULL); { IMessagePtr iMsg(__uuidof(Message)); IBodyPartPtr iBp; IBodyPartPtr iBp1; IBodyPartPtr iBp2; FieldsPtr Flds; _StreamPtr Stm; /* ** multipart/mixed (root) */ iBp = iMsg; Flds = iBp->Fields; Flds->Item["urn:schemas:mailheader:content-type"]->Value = _bstr_t("multipart/mixed"); Flds->Update(); /* ** multipart/alternative (child of root) */ iBp1 = iBp->AddBodyPart(-1); // multipart/altenative body part /* ** text/plain */ iBp2 = iBp1->AddBodyPart(-1); // text/plain body part Flds = iBp2->Fields; Flds->Item["urn:schemas:mailheader:content-type"]->Value = _bstr_t("text/plain"); Flds->Item["urn:schemas:mailheader:content-transfer-encoding"]->Value = _bstr_t("quoted-printable"); Flds->Update(); Stm = iBp2->GetDecodedContentStream(); Stm->WriteText(_bstr_t("text version of message"),stWriteChar); Stm->Flush(); /* ** text/html */ iBp2 = iBp1->AddBodyPart(-1); // text/html body part Flds = iBp2->Fields; Flds->Item["urn:schemas:mailheader:content-type"]->Value = _bstr_t("text/html"); Flds->Item["urn:schemas:mailheader:content-transfer-encoding"]->Value = _bstr_t("quoted-printable"); Flds->Update(); Stm = iBp2->GetDecodedContentStream(); Stm->WriteText(_bstr_t("<html><p><em>html</em> version of message</p></html>"),stWriteChar); Stm->Flush(); /* ** set multipart/alternative header */ Flds = iBp1->Fields; Flds->Item["urn:schemas:mailheader:content-type"]->Value = _bstr_t("multipart/alternative"); Flds->Update(); /* ** image/gif (child of root multipart/mixed) */ iBp1 = iBp->AddBodyPart(-1); Flds = iBp1->Fields; Flds->Item["urn:schemas:mailheader:content-type"]->Value = _bstr_t("image/gif"); Flds->Item["urn:schemas:mailheader:content-transfer-encoding"]->Value = _bstr_t("base64"); Flds->Update(); Stm = iBp1->GetDecodedContentStream(); Stm->LoadFromFile(_bstr_t("myimage.gif")); Stm->Flush(); /* ** Save message stream to disk */ Stm = iMsg->GetStream(); Stm->SaveToFile(_bstr_t("message.eml"),adSaveCreateOverWrite); } CoUninitialize(); }
Dim iMsg Set iMsg = CreateObject("CDO.Message") Dim iBp Dim iBp1 Dim iBp2 Dim Flds Dim Stm Set iBp = iMsg ' get IBodyPart on Message object Set Flds = iBp.Fields Flds("urn:schemas:mailheader:content-type") = "multipart/mixed" Flds.Update Set iBp1 = iBp.AddBodyPart ' Add a BodyPart object to the hierarchy Set iBp2 = iBp1.AddBodyPart ' text/plain Set Flds = iBp2.Fields Flds("urn:schemas:mailheader:content-type") = "text/plain" Flds("urn:schemas:mailheader:content-transfer-encoding") = "quoted-printable" Flds.Update Set Stm = iBp2.GetDecodedContentStream Stm.WriteText "This is a message" Stm.Flush Set iBp2 = iBp1.AddBodyPart ' text/html Set Flds = iBp2.Fields Flds("urn:schemas:mailheader:content-type") = "text/html" Flds("urn:schemas:mailheader:content-transfer-encoding") = "quoted-printable" Flds.Update Set Stm = iBp2.GetDecodedContentStream Stm.WriteText "<html><h1>This is a message</h1></html>" Stm.Flush ' This may appear out of order, but we have to _reset_ the ' content-type for the "multipart/alternative" body part after adding ' the two representations. If we set it before, it will be switched ' to "multipart/mixed" when we call the AddBodyPart() method on the ' object. Set Flds = iBp1.Fields Flds("urn:schemas:mailheader:content-type") = "multipart/alternative" Flds.Update Set iBp1 = iBp.AddBodyPart ' image/gif Set Flds = iBp1.Fields Flds("urn:schemas:mailheader:content-type") = "image/gif" Flds("urn:schemas:mailheader:content-transfer-encoding") = "base64" Set Stm = iBp1.GetDecodedContentStream Stm.LoadFromFile "c:\images\myimage.gif" Strm.Flush