Gmail 问题 - 使用 MailKit 嵌入图像

Issue with Gmail - Embedded images using MailKit(Gmail 问题 - 使用 MailKit 嵌入图像)
本文介绍了Gmail 问题 - 使用 MailKit 嵌入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 MailKit 发送带有嵌入图像的电子邮件.它在 MS Outlook 上运行良好.但是,图像不会在 Gmail 中显示为嵌入图像.我尝试使用data:image/png;base64"格式附加到它,它也适用于 Outlook,但不适用于 Gmail.

I'm trying to send emails with embedded images using MailKit. It works well on MS Outlook. However, images do not display as embedded images in Gmail. I tried to attach with "data:image/png;base64," format to it also worked on Outlook but not Gmail.

任何帮助将不胜感激!

public string SendEmail (MyModel myModel)
{
    var message = new MimeMessage();
    var bodyBuilder = new BodyBuilder
    {
        HtmlBody = myModel.Body
    };
    GetAllImgTags(bodyBuilder);
    message.Body = bodyBuilder.ToMessageBody();
    // Send Email Here...
    return "OK"
}

    public string GetAllImgTags(BodyBuilder bodyBuilder)
    {
        HtmlDocument document = new HtmlDocument();

        document.LoadHtml(bodyBuilder.HtmlBody);

        var imgList = document.DocumentNode.Descendants("img").Where(x =>
        {
            string src = x.GetAttributeValue("src", null) ?? "";
            return !string.IsNullOrEmpty(src);
        }).ToList();

        foreach(var item in imgList)
        {
            string currentSrcValue = item.GetAttributeValue("src", null);
            var file = Path.Combine(_env.WebRootPath,"images", currentSrcValue);
            if (File.Exists(file))
            {                    
                byte[] imageData = System.IO.File.ReadAllBytes(file);
                string contentId = string.Format("{0}@{1}", Path.GetFileName(file), Guid.NewGuid().ToString());
                LinkedResource inline = new LinkedResource(new MemoryStream(imageData), Image.Jpeg)
                {
                    ContentId = contentId,
                    TransferEncoding = TransferEncoding.Base64,
                    ContentLink = new Uri("cid:" + contentId),

                };
                inline.ContentType.Name = contentId;
                inline.ContentType.MediaType = Image.Jpeg;
                bodyBuilder.LinkedResources.Add(contentId, new MemoryStream(imageData));

                item.SetAttributeValue("src", "cid:" + contentId);
                inline.Dispose();
            }
        }

        bodyBuilder.HtmlBody = document.DocumentNode.OuterHtml;
        string result = document.DocumentNode.OuterHtml;
        return result;
    }

推荐答案

首先,你为什么要创建 System.Net.Mail 对象,然后简单地处理它们而不以任何方式使用它们?

Well, first of all, why are you creating System.Net.Mail objects and then simply disposing them without using them in any way?

请参阅此代码以了解我的意思:

See this code for what I mean:

LinkedResource inline = new LinkedResource(new MemoryStream(imageData), Image.Jpeg)
{
    ContentId = contentId,
    TransferEncoding = TransferEncoding.Base64,
    ContentLink = new Uri("cid:" + contentId),
};
inline.ContentType.Name = contentId;
inline.ContentType.MediaType = Image.Jpeg;

让我们试试这个:

var contentType = new ContentType ("image", "jpeg");
var contentId = MimeKit.Utils.MimeUtils.GenerateMessageId ();
var image = (MimePart) bodyBuilder.LinkedResources.Add (file, contentType);
image.ContentTransferEncoding = ContentEncoding.Base64;
image.ContentId = contentId;

item.SetAttributeValue ("src", "cid:" + contentId);

这篇关于Gmail 问题 - 使用 MailKit 嵌入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)