简体   繁体   English

将图像嵌入电子邮件正文

[英]Embedding an image in email body

I am creating an add in for outlook 2007, and what i am trying to do is embed an image to a new email. 我正在为Outlook 2007创建一个插件,而我试图做的就是将图像嵌入到新电子邮件中。 I cannot get the embedding of the image to work, please help. 我无法嵌入图片,请提供帮助。 My code is as follows: 我的代码如下:

 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>";
            }

        }
    }

but this is not displaying the image. 但这不显示图像。 Please help. 请帮忙。

Thanks in advance. 提前致谢。

While I haven't used the Outlook components directly, you're going to need to embed the image in the mail. 虽然我没有直接使用Outlook组件,但是您需要将图像嵌入到邮件中。 All you are doing in the code above is creating a string that references an image on your local hard drive. 您在上面的代码中所做的全部工作就是创建一个引用本地硬盘上图像的字符串。

In my world, I iuse the .NET mail components, so take this with a grain of salt , but the concepts should transfer. 在我的世界中,我使用.NET邮件组件, 因此请耐心等待,但是概念应该可以转移。 I do something like this: 我做这样的事情:

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);

Also, when creating HTML Messages it is considered good practice to include a plain text version. 同样,在创建HTML消息时,包括纯文本版本也被认为是一种好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM