Issue with Gmail - Embedded images using 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 嵌入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Gmail 问题 - 使用 MailKit 嵌入图像
 
				
         
 
            
        - C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
 
						 
						 
						 
						 
						 
				 
				 
				 
				