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 嵌入图像
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01