如何使用 .NET 在电子邮件正文中嵌入多个图像 [英] How to embed multiple images in email body using .NET

查看:55
本文介绍了如何使用 .NET 在电子邮件正文中嵌入多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序向用户发送电子邮件,其中嵌入了电子邮件正文 (HTML) 中的多个图像(图表).

I'm writing a program that sends emails to users with multiple images (charts) embedded in the Email message body (HTML).

当我尝试位于此处的示例时..当我只需要嵌入一张图像时效果很好http://www.systemnetmail.com/faq/4.4.aspx.

When I tried the sample located here..which worked well when I have to embed only one image http://www.systemnetmail.com/faq/4.4.aspx.

但是,当我尝试使用以下代码嵌入多个图像时,没有嵌入任何图像,而是将它们作为附件发送.

But, when i tried to embed multiple images using the below code, none of the images are being embedded , instead they are sent as attachments.

public MailMessage MailMessage(Metric metric, DateTime date)
{
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress("test@gmail.com", "User1");
    msg.To.Add(new MailAddress("test@gmail.com"));
    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 源代码如下:

Any clues as what I'm missing? I checked the .htm file which is also sent as attachment with the email, and html source looks as follows:

<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 body 中发送多个图像,而不是作为附件.

So the Q is how to send multiple images in the html body , not as attachment.

推荐答案

所以,我想弄清楚实际问题是什么它在这一行

So, I think figured out what the actual problem is Its in this line

// Alternate view for embedded images
    AlternateView avText = AlternateView.CreateAlternateViewFromString(metric.Name, null, MediaTypeNames.Text.Html);
    AlternateView avImages = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);

如您所见,我的两个视图都指定为 Text.Html,因此第一个视图覆盖了下一个视图,因此我只看到文本和图像作为附件发送

As you can see, both my views are specified as Text.Html, so the the 1st one is overriding the next one and so I only see text and images are sent as attachments

我进行了以下更改并按预期工作

I made the following change and it worked as expected

AlternateView avText = AlternateView.CreateAlternateViewFromString(metric.Name, null, **MediaTypeNames.Text.Plain**);
AlternateView avImages = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);

这篇关于如何使用 .NET 在电子邮件正文中嵌入多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆