我正在编写一个程序,将电子邮件发送给嵌入电子邮件正文(HTML)中的多个图像(图表)的用户。如何使用.NET在电子邮件正文中嵌入多个图像
当我尝试位于这里的示例..当我只需要嵌入一个图像 http://www.systemnetmail.com/faq/4.4.aspx。
但是,当我试图嵌入多个图像使用下面的代码,没有任何图像被嵌入,而是他们作为附件发送。
public MailMessage MailMessage(Metric metric, DateTime date)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]", "User1");
msg.To.Add(new MailAddress("[email protected]"));
msg.Subject = "Trend for metric: " + metric.Name;
msg.IsBodyHtml = true;
// Generate the charts for the given metric
var charts = this.GenerateCharts(metric, date);
int i = 0;
string htmlBody = "<html><body>";
List<LinkedResource> resources = new List<LinkedResource>();
foreach (var chart in charts)
{
string imageTag = string.Format("<img src=cid:chart{0} /><br>", i);
htmlBody += imageTag;
LinkedResource graph = new LinkedResource(chart.Value, "image/jpeg");
graph.ContentId = "chart" + i;
resources.Add(graph);
i++;
}
htmlBody += "</body></html>";
// Alternate view for embedded images
AlternateView avText = AlternateView.CreateAlternateViewFromString(metric.Name, null, MediaTypeNames.Text.Html);
AlternateView avImages = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
// Add all the images as linked resources
resources.ForEach(x => avImages.LinkedResources.Add(x));
// Add the views for image
msg.AlternateViews.Add(avText);
msg.AlternateViews.Add(avImages);
return msg;
}
作为我失踪的任何线索? 我检查它也被作为与电子邮件附件中的.htm文件,HTML源代码如下所示:
<html>><body><img src=cid:chart0 /><br><img src=cid:chart1 /><br><img src=cid:chart2/><br><img src=cid:chart3 /><br><img src=cid:chart4 /><br></body></html>
所以Q是如何在HTML正文发送多张图片,而不是作为附件。
可能的重复[发送电子邮件与图像嵌入在身体从C#](http://stackoverflow.com/questions/1921275/sending-an-email-with-an-image-embedded-in-the- body-from-c) –