我使用以下代码将图像添加到 RDOMessage 并尝试在 HTMLBody 中显示该图像...
_message.BodyFormat = 2;
_message.HTMLBody = MailHelper.GetHtmlMessage(MailBody, "utf-8");
if (ImageSource != null && !string.IsNullOrWhiteSpace(ImageName))
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(ImageSource));
string id = "qr-code.png";
int index = _message.HTMLBody.IndexOf("%imageQr%");
if (index >= 0)
{
string tempDir = TempDirectory.Create();
string tmpFile = Path.Combine(tempDir, id);
using (FileStream fileStream = new FileStream(tmpFile, FileMode.Create))
{
encoder.Save(fileStream);
}
RDOAttachment attachment = _message.Attachments.Add(tmpFile, 1, null, ImageNage);
attachment.ContentID = id;
mMessage.HTMLBody = _message.HTMLBody.Insert(index, $"<img src=\"cid:{id}\">");
mMessage.HTMLBody = _message.HTMLBody.Replace("%imageQr%", string.Empty);
}
}
所以问题是:什么是不正确的? (图像前后各有三个下划线,这不是问题;))我在与 OOM 版本略有不同的版本中使用它并且效果很好...... 有趣的是,对于 OOM,我可以将
id
指定为没有文件名结尾的 Guid
(即没有 .png),如果我在这里使用 RDO 这样做,附件本身也无法打开...
cid 应该放在引号内:
mMessage.HTMLBody = _message.HTMLBody.Insert(index, $"<img src=\"cid:{id}\">";);