在.NET / C#3.5中我使用嵌入图像构建电子邮件,在 smtp4dev (http://smtp4dev.codeplex.com/)中收到电子邮件,但是图像不会显示。 我使用HTML Agility Pack来解析HTML文档并进行一些清理(这部分工作正常)。
//uninteresting code above
var images = doc.DocumentNode.Descendants("img");
List<MemoryStream> listOfStreams = new List<MemoryStream>();
List<string> listOfCid = new List<string>();
List<string> listOfReplacedSrcValues = new List<string>();
foreach (var img in images)
{
var src = img.Attributes["src"];
if (src != null && src.Value.StartsWith("http://"))
{
listOfReplacedSrcValues.Add(src.Value);
//I build a string that looks like this inv_brandLogo_fr_gif
string cid = src.Value.Substring(src.Value.LastIndexOf('/'), src.Value.Length - src.Value.LastIndexOf('/')).Trim('/').Replace('.', '_');
string cidWithTimestamp = string.Format("cid:{0}_{1}", cid, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fffffff")); //append a timestamp to ensure the cid is unique !
PageResult = PageResult.Replace(src.Value, cidWithTimestamp);
listOfCid.Add(cidWithTimestamp);
WebClient wc = new WebClient();
byte[] originalData = wc.DownloadData(src.Value);
MemoryStream ms = new MemoryStream(originalData);
listOfStreams.Add(ms);
}
}
MailAlert.SendRecap(Context.User.Identity.Name, PageResult, listOfStreams, listOfCid);
//end of the 1st bit of code
public static void SendRecap(string connectedUser, string HTMLcontent, List<MemoryStream> listOfStreams, List<string> listOfCid)
{
try
{
SmtpClient sender = new SmtpClient();
MailMessage message = new MailMessage();
message.From = new MailAddress("recap@test.com");
message.To.Add(string.Format("{0}@vente-privee.com", connectedUser));
message.Subject = "Test Recap";
message.Body = HTMLcontent;
message.IsBodyHtml = true;
int i = 0;
string plainBody = "Plain text content, viewable by clients that don\'t support html";
AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, null, "text/plain");
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(HTMLcontent, null, "text/html");
//create the LinkedResource (embedded image)
foreach (var ms in listOfStreams)
{
ContentType ct = new ContentType();
if (listOfCid[i].Contains("gif"))
{
ct = new ContentType(MediaTypeNames.Image.Gif);
}
else if (listOfCid[i].Contains("jpg") || listOfCid[i].Contains("jpeg"))
{
ct = new ContentType(MediaTypeNames.Image.Jpeg);
}
else if (listOfCid[i].Contains("png"))
{
//contentType = "image/png";
ct = new ContentType(MediaTypeNames.Image.Jpeg);
}
LinkedResource imageResource = new LinkedResource(ms, ct);
imageResource.ContentId = listOfCid[i];
imageResource.TransferEncoding = TransferEncoding.Base64;
htmlView.LinkedResources.Add(imageResource);
i++;
}
message.AlternateViews.Add(plainView);
message.AlternateViews.Add(htmlView);
sender.Send(message);
}
catch (Exception ex)
{
throw ex;
}
}
当我在smtp4dev中检查收到的电子邮件时,我可以看到左栏“MIME部分”中的所有部分:
我甚至可以选择任何这些图像,单击“正文”选项卡,选择“另存为”并在我最喜欢的图像查看器中成功打开它们。
当我选择“未命名:text / html(50642字节)”行然后选择Body选项卡时,我可以看到电子邮件的HTML源代码和所有“cid:”(例如:src =“cid:inv_brandLogo_fr_gif_2013- 01-04-18-50-34-4569409" )
但如果我点击smtp4dev中的“打开”,则电子邮件会在Internet Explorer中打开,而不显示任何图像。
当IE打开时,它会在底部显示一条警告:“Internet Explorer限制此网页运行脚本或ActiveX控件”,并点击“允许阻止的内容”按钮,但无效...
我读过很多网站/博客,但无法弄清楚嵌入图片无法显示的原因。 我在这里缺少什么?