Gmail 问题 - 使用 MailKit 嵌入图像
Issue with Gmail - Embedded images using MailKit
我正在尝试使用 MailKit 发送带有嵌入图像的电子邮件。它适用于 MS Outlook。但是,图像不会在 Gmail 中显示为嵌入图像。我尝试以 "data:image/png;base64," 格式附加到它也适用于 Outlook,但不适用于 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 对象然后简单地处理它们而不以任何方式使用它们?
看这段代码了解我的意思:
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);
我正在尝试使用 MailKit 发送带有嵌入图像的电子邮件。它适用于 MS Outlook。但是,图像不会在 Gmail 中显示为嵌入图像。我尝试以 "data:image/png;base64," 格式附加到它也适用于 Outlook,但不适用于 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 对象然后简单地处理它们而不以任何方式使用它们?
看这段代码了解我的意思:
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);