在电子邮件正文中嵌入图像

本文关键字:图像 电子邮件 正文 | 更新日期: 2023-09-27 18:21:57

我正在为outlook 2007创建一个外接程序,我想做的是将一个图像嵌入到一封新的电子邮件中。我无法使图像的嵌入工作,请帮助。我的代码如下:

 private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Outlook.Inspectors inspectors;
        inspectors = this.Application.Inspectors;
        inspectors.NewInspector +=
        new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
    }
    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }
    void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
    {
        Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
        if (mailItem != null)
        {
            if (mailItem.EntryID == null)
            {
                mailItem.Subject = "This text was added by using code";
                mailItem.HTMLBody = "<html><body>this is a <img src=" + @"C:'Users'Public'Pictures'Sample Pictures'Chrysanthemum.jpg> embedded picture.</body></html>";
                //mailItem.HtmlBody = "<html><body>this is a <img src='"cid:" + @"C:'Users'Public'Pictures'Sample Pictures'Chrysanthemum.jpg" + "'"> embedded picture.</body></html>";
            }
        }
    }

但是这不是在显示图像。请帮忙。

提前谢谢。

在电子邮件正文中嵌入图像

虽然我没有直接使用Outlook组件,但您需要将图像嵌入邮件中。在上面的代码中,您所要做的就是创建一个引用本地硬盘上的映像的字符串。

在我的世界里,我使用.NET邮件组件,所以对此持保留态度,但概念应该转移。我这样做:

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(model.MessageBody_Html, null, MediaTypeNames.Text.Html);
ImgStream   = new MemoryStream(media.MediaData); 
linkedImage = new LinkedResource(ImgStream, MediaTypeNames.Image.Jpeg);
linkedImage.ContentId    = "img_" + media.MediaID;
linkedImage.TransferEncoding    = TransferEncoding.Base64;
htmlView.LinkedResources.Add(linkedImage);

此外,在创建HTML消息时,包含纯文本版本被认为是一种很好的做法。