【问题标题】:No embedded images in email电子邮件中没有嵌入图像
【发布时间】:2013-05-04 01:27:18
【问题描述】:

我尝试发送带有嵌入图像的电子邮件。图片来自List<Bitmap>,我确信它们 100% 存在。

不知何故,当我收到电子邮件时,我根本看不到任何图像,而 HTML 看起来像

<img alt="" hspace="0" src="http://image0" align="baseline" border="0">
<br />

<img alt="" hspace="0" src="http://image1" align="baseline" border="0">
<br />

有什么线索吗?

C# 就像

var smtp = new SmtpClient();

var msg = new MailMessage(new MailAddress("support@mysite.com", "mysite.com Support"),
                                new MailAddress(email, email));
msg.Subject = "No worries, man";
msg.IsBodyHtml = true;
var bodyBuilder = new StringBuilder();

for (int i = 0; i < pages.Count; i++)
   bodyBuilder.AppendLine(string.Format("<img alt=\"\" hspace='0' src='cid:image{0}' align='baseline' border='0'><br />",i));
var htmlView = AlternateView.CreateAlternateViewFromString(bodyBuilder.ToString(), null, "text/html");

var index = 0;
foreach (var page in pages) // where page is List<Bitmap>
{
   var memoryStream = new MemoryStream();
   page.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);

   var imagelink = new LinkedResource(memoryStream, "image/png");
   imagelink.ContentId = string.Format("image{0}", index); 
   imagelink.TransferEncoding = TransferEncoding.Base64;

   htmlView.LinkedResources.Add(imagelink);
   index++;
}

msg.AlternateViews.Add(htmlView);

try
{
    smtp.Send(msg);
    return Json("true", JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
   logger.Error(ex.Message, ex);

   return Json("false", JsonRequestBehavior.AllowGet);
}

【问题讨论】:

  • img 元素上的 html src 标签看起来不正确。
  • @gwin003 ...好吧...在嵌入图像的情况下应该如何?你能建议吗?
  • @MichaelTodd 对不起......一切都在代码中,你能看一下吗?
  • mm...为什么不在 htmlview 中设置编码类型?像 AlternateView htmlView = AlternateView.CreateAlternateViewFromString(bodyBuilder.ToString(), Encoding.UTF8, MediaTypeNames.Text.Html);
  • 调试时,img src中的字符串在什么时候从cid:变为http:

标签: c# .net email html-email


【解决方案1】:

大家好

我在这里找到了很棒的解决方案

How to embed an Image Stream to MailMessage

这是正确的代码:

ImageConverter ic = new ImageConverter();
Byte [] ba = (Byte[]) ic.ConvertTo(page,typeof(Byte[]));
var memoryStream = new MemoryStream(ba);
page.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);

【讨论】:

    猜你喜欢
    • 2011-10-30
    • 2012-07-29
    • 2011-10-06
    • 2015-01-29
    • 2014-10-03
    • 2015-03-14
    • 2016-11-06
    • 2012-04-07
    相关资源
    最近更新 更多