C# 电子邮件中嵌入的图像未显示(.NET 3.5)
在.NET/C#3.5中,我构建了一封带有嵌入图像的电子邮件,在smtp4dev中接收电子邮件(http://smtp4dev.codeplex.com/)但是这些图像不会显示出来。 我使用HTMLAgility包解析HTML文档并进行一些清理(这部分很好)C# 电子邮件中嵌入的图像未显示(.NET 3.5),c#,.net,email,smtp,alternateview,C#,.net,Email,Smtp,Alternateview,在.NET/C#3.5中,我构建了一封带有嵌入图像的电子邮件,在smtp4dev中接收电子邮件(http://smtp4dev.codeplex.com/)但是这些图像不会显示出来。 我使用HTMLAgility包解析HTML文档并进行一些清理(这部分很好) //上面没有意思的代码 var images=doc.DocumentNode.substands(“img”); List listOfStreams=新列表(); List listOfCid=新列表(); ListListOfRepl
//上面没有意思的代码
var images=doc.DocumentNode.substands(“img”);
List listOfStreams=新列表();
List listOfCid=新列表();
ListListOfReplacedSrcValues=新列表();
foreach(图像中的var img)
{
var src=img.Attributes[“src”];
if(src!=null&&src.Value.StartsWith(“http://”)
{
replacedsrcvalues.Add(src.Value)列表;
//我构建了一个字符串,看起来像这个inv\u brandLogo\u fr\u gif
字符串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”);//附加时间戳以确保cid是唯一的!
PageResult=PageResult.Replace(src.Value,cidWithTimestamp);
添加(cidWithTimestamp);
WebClient wc=新的WebClient();
字节[]originalData=wc.DownloadData(src.Value);
MemoryStream ms=新的MemoryStream(原始数据);
流列表。添加(ms);
}
}
SendRecap(Context.User.Identity.Name、PageResult、listOfStreams、listOfCid);
//代码的第一位结束
公共静态void sendecap(字符串connectedUser、字符串HTMLcontent、流列表、ID列表)
{
尝试
{
SmtpClient sender=新的SmtpClient();
MailMessage=新的MailMessage();
message.From=新邮件地址(“recap@test.com");
message.To.Add(string.Format(“{0}@vente privee.com”,connectedUser));
message.Subject=“测试重述”;
message.Body=HTMLcontent;
message.IsBodyHtml=true;
int i=0;
string plainBody=“纯文本内容,可由不支持html的客户端查看”;
AlternateView plainView=AlternateView.CreateAlternateView-FromString(plainBody,null,“文本/普通”);
AlternateView htmlView=AlternateView.CreateAlternateView-FromString(HTMLcontent,null,“text/html”);
//创建LinkedResource(嵌入图像)
foreach(listOfStreams中的var ms)
{
ContentType ct=新的ContentType();
if(listOfCid[i]。包含(“gif”))
{
ct=新的ContentType(MediaTypeNames.Image.Gif);
}
else if(listOfCid[i].Contains(“jpg”)| | listOfCid[i].Contains(“jpeg”))
{
ct=新的ContentType(MediaTypeNames.Image.Jpeg);
}
else if(listOfCid[i].Contains(“png”))
{
//contentType=“image/png”;
ct=新的ContentType(MediaTypeNames.Image.Jpeg);
}
LinkedResource imageResource=新的LinkedResource(ms、ct);
imageResource.ContentId=listOfCid[i];
imageResource.Transferncode=Transferncode.Base64;
htmlView.LinkedResources.Add(imageResource);
i++;
}
message.AlternateViews.Add(plainView);
message.AlternateViews.Add(htmlView);
发送者。发送(消息);
}
捕获(例外情况除外)
{
掷骰子;
}
}
当我在smtp4dev中检查收到的电子邮件时,我可以在左侧栏“MIME部分”中看到其所有部分:
- 未命名:多部分/可选(158959字节)
- 未命名:文本/普通(50642字节)
- 未命名:文本/普通(65字节)
- 未命名:多部分/相关(107752字节)
- 未命名:文本/html(50642字节)
- 未命名:图像/gif(4610字节)
- 未命名:图像/jpeg(1908字节)
- 未命名:图像/gif(540字节)
- 未命名:图像/gif(544字节)
- 未命名:文本/html(48466字节)
我在这里遗漏了什么?看起来您并没有实际更改html代码中src属性的值。您在电子邮件中正确地嵌入了图像,但是您在电子邮件的html正文中引用了它们吗?如果我正确地理解了我阅读的所有示例,我需要在成功的html源中每个src都有一个唯一的cid值。正如我所说,在我的最终html源中,例如,
和smtp4dev显示在未命名的:image/gif(4610字节)元素的标题选项卡中:content type=image/gif | content transfer encoding=base64 | content id=我刚刚成功
//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;
}
}