cdo发送图片邮件

来源:互联网 发布:java api手机版闪退 编辑:程序博客网 时间:2024/06/02 12:41

如何用CDOSYS发送带图片邮件,而且不是用附件的形式?我google (www.google.com) 了一下,发现资料比较少,而且msdn(http://msdn.microsoft.com) 上关于cdo的问答个不是很详细,不过最终在

www.codeproject.com发现了一位老兄写的一段转换html --> mht的代码,就“剽窃“了过来,修改了一下,大功告成:),下面就是核心代码:

                  

CDO.MessageClass msg =MailMsg;

                   ADODB.Stream stm = null;

                   System.IO.MemoryStream MS = null;                      

                   CDO.IBodyPart iBp;

                   CDO.IBodyPart mainBody = msg;

                   //Makea multipart mhtml document

                   mainBody.ContentMediaType ="multipart/related";                

                   stringhtml = "<html><body><p>HELLO ST</p><img src=012.jpg></body></html>";

                   //Makethe html part of the document

                   iBp =mainBody.AddBodyPart(-1);

                   iBp.ContentMediaType ="text/html";

                   iBp.ContentTransferEncoding ="quoted-printable";

                   stm =iBp.GetDecodedContentStream();

                   stm.WriteText(html,ADODB.StreamWriteEnum.stWriteChar);

                   stm.Flush();

                   //Makethe image parts of the document

                   iBp =mainBody.AddBodyPart(-1);

                   iBp.ContentMediaType ="image/jpeg";

                   iBp.ContentTransferEncoding ="base64";

                   //ContentLocationmust be the same as in the html part to make them linked

                   iBp.Fields.Append("urn:schemas:mailheader:content-location",

                       ADODB.DataTypeEnum.adBSTR,

                       0,

                       ADODB.FieldAttributeEnum.adFldIsNullable,

                       "012.jpg");

                   iBp.Fields.Update();

                   iBp.Fields.Refresh();

                   MS = newMemoryStream();

                   System.Drawing.Image img =System.Drawing.Image.FromFile(@"c:/012.jpg");

                   img.Save(MS,System.Drawing.Imaging.ImageFormat.Jpeg);                

                   stm =iBp.GetDecodedContentStream();              

                   stm.Write(MS.ToArray());

                   stm.Flush();

                   MS.Close();

                   stm.Close();

原创粉丝点击