我正在編寫一個程序,將電子郵件發送給嵌入電子郵件正文(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) –