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

发布于 2025-01-01 15:31:57 字数 1314 浏览 5 评论 0原文

我正在为 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>";
            }

        }
    }

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

提前致谢。

I am creating an add in for outlook 2007, and what i am trying to do is embed an image to a new email. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风启觞 2025-01-08 15:31:57

虽然我没有直接使用 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 消息时,包含纯文本版本被认为是很好的做法。

While I haven't used the Outlook components directly, you're going to need to embed the image in the mail. 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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文