前言
上篇提到如果邮件中有图片的话,可以使用 @Html.EmbedImage("~/Content/postal.png") 这种方式,但是经过测试发现,在outlook中如果有该行代码,则会导致乱码问题,原因未知(有兴趣可研究下)。。。
后台以附件形式添加图片
下面提供另一种方式,解决outlook乱码问题,,,
后台代码,以附件的形式
public ActionResult SendImageEmail()
{ var emailService = new EmailService(ViewEngines.Engines, () => CreateMySmtpClient()); dynamic email = new Email("SimpleImage"); var img = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", "Content", "postal.png");//"bin",
var imageAttachment = new Attachment(img);
email.Image = imageAttachment.ContentId;
email.Attach(imageAttachment); emailService.Send(email); return RedirectToAction("Sent", "Home"); }
private SmtpClient CreateMySmtpClient()
{
SmtpClient mailClient = new SmtpClient("localhost");
return mailClient; }
邮件模板代码
To: test@example.org
From: test@example.org
CC:cc@example.org
BCC:BCC@example.org
Subject: Simple email example <html>
<body>
<p>你好,Postal</p>
<p>This is an <code>HTML</code> message</p>
<p>Generated by <a href="http://aboutcode.net/postal">Postal</a> on @ViewBag.Date</p>
<p>
<img src="cid:@Model.Image" />
</p>
<p>
The date is: @ViewBag.Date
</p>
</body>
</html>
记着图片设置为 如果较新则复制