c# - 使用 C# Windows 应用程序在电子邮件正文中添加多个图像(内联)

标签 c# .net winforms

我搜索了好几次并找到了解决方案,但都只支持一张图像。最后我使用了这段代码。 但问题是,如果 html 包含多个图像,则正文中只会显示一张图像,其他图像将作为附件显示。

string inputHtmlContent = htmlbody;
string outputHtmlContent = string.Empty;
var myResources = new List<LinkedResource>();

if ((!string.IsNullOrEmpty(inputHtmlContent)))
{
  var doc = new HtmlAgilityPack.HtmlDocument();
  doc.LoadHtml(inputHtmlContent);
  HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//img");
  if (nodes !=null)
  {
    foreach (HtmlNode node in nodes)
    {
      if (node.Attributes.Contains("src"))
      {
        string data = node.Attributes["src"].Value;
        string imgPath = Application.StartupPath+"\\"+data;
        var imgLogo = new LinkedResource(imgPath);
        imgLogo.ContentId = Guid.NewGuid().ToString();
        imgLogo.ContentType = new ContentType("image/jpeg");
        myResources.Add(imgLogo);
        node.Attributes["src"].Value = string.Format("cid:{0}", imgLogo.ContentId);
        outputHtmlContent = doc.DocumentNode.OuterHtml;
      }
    }
  }
  else
  {
    outputHtmlContent = inputHtmlContent;
  }
  AlternateView av2 = AlternateView.CreateAlternateViewFromString(outputHtmlContent,
                            null, MediaTypeNames.Text.Html);
  foreach (LinkedResource linkedResource in myResources)
  {
    av2.LinkedResources.Add(linkedResource);
  }

  msg.AlternateViews.Add(av2);

请帮我解决这个问题,如何显示电子邮件正文中的所有图像?...

最佳答案

您可以将图像附加到邮件中,然后放置 img 标签并使用 ContentId附件作为src这样:

private void denMailButton_Click(object sender, EventArgs e)
{
    string subject = "Subject";
    string body = @"Image 1: <img src=""$CONTENTID1$""/> <br/> Image 2: <img src=""$CONTENTID2$""/> <br/> Some Other Content";

    MailMessage mail = new MailMessage();
    mail.From = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4224302d2f02273a232f322e276c212d2f" rel="noreferrer noopener nofollow">[email protected]</a>");
    mail.To.Add(new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdb9a28da8b5aca0bda1a8e3aea2a0" rel="noreferrer noopener nofollow">[email protected]</a>"));
    mail.Subject = subject;
    mail.Body = body;
    mail.Priority = MailPriority.Normal;

    string contentID1 = Guid.NewGuid().ToString().Replace("-", "");
    string contentID2 = Guid.NewGuid().ToString().Replace("-", "");

    body = body.Replace("$CONTENTID1$", "cid:" + contentID1);
    body = body.Replace("$CONTENTID2$", "cid:" + contentID2);

    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");

    //path of image or stream
    LinkedResource imagelink1 = new LinkedResource(@"D:\1.png", "image/png");
    imagelink1.ContentId = contentID1;
    imagelink1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
    htmlView.LinkedResources.Add(imagelink1);

    LinkedResource imagelink2 = new LinkedResource(@"D:\2.png", "image/png");
    imagelink2.ContentId = contentID2;
    imagelink2.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
    htmlView.LinkedResources.Add(imagelink2);

    mail.AlternateViews.Add(htmlView);

    SmtpClient client = new SmtpClient();
    client.Host = "mail.example.com";
    client.Credentials = new NetworkCredential("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86e0f4e9ebc6e3fee7ebf6eae3a8e5e9eb" rel="noreferrer noopener nofollow">[email protected]</a>", "password");
    client.Send(mail);
}

这是屏幕截图:

enter image description here

关于c# - 使用 C# Windows 应用程序在电子邮件正文中添加多个图像(内联),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33665280/

相关文章:

c# - 如何通过脚本模拟 AWS Toolkit for Visual Studio 中的构建和部署功能?

c# - 不使用 Code First 时良好的通用存储库模式

c# - 在 C# 中使用反射进行转换

c# - Directory.GetFiles 使用 SearchPattern 返回意外结果

c# - 检查变量是否为空c#

c# - 如何使用服务器模式在 GridView 中获取异常详细信息?

c# - HttpContext.Current.Session 给出 StackOverflowException?

.net - 将结果值转换为 Linq 中的字符串列表

c# - 如何在固定大小的 ToolStripDropDown 中滚动

c# - 如何在不显示/显示的情况下打印 Windows 窗体