【发布时间】:2013-08-23 21:22:11
【问题描述】:
在通过电子邮件将图像作为正文中的嵌入图像发送时遇到问题。 图像文件显示为附件,这是可以的,但内联图像部分仅显示为红色 x。
这是我目前所拥有的
LinkedResource inline = new LinkedResource(filePath);
inline.ContentId = Guid.NewGuid().ToString();
MailMessage mail = new MailMessage();
Attachment att = new Attachment(filePath);
att.ContentDisposition.Inline = true;
mail.From = from_email;
mail.To.Add(data.email);
mail.Subject = "Client: " + data.client_id + " Has Sent You A Screenshot";
mail.Body = String.Format(
"<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
@"<img src=""cid:{0}"" />", inline.ContentId);
mail.IsBodyHtml = true;
mail.Attachments.Add(att);
【问题讨论】:
-
您实际上并没有将 LinkedResource 附加到邮件对象;相反,您正在创建它,然后附加一个单独的 Attachment 对象。
-
这段代码唯一的问题是你的 string.Format 引用了
inline.ContentId,而实际上它应该是att.ContentId。inline根本不需要。我更喜欢你的问题而不是所有的答案,因为你真的不需要使用AlternateView。 -
我的图像被附加为 bin 文件扩展名。我做错了吗?
-
查看此链接。它已经准备好使用多个内联附件以及 pdf/excel 文件的一般附件的方法。 stackoverflow.com/questions/33665280/…
标签: c# image email smtp inline