我搜索了几次,找到了解决方案,但是所有解决方案都只支持一张图像。最后我使用了这段代码。
但是问题是,如果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("from@example.com");
    mail.To.Add(new MailAddress("to@example.com"));
    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("from@example.com", "password");
    client.Send(mail);
}


这是屏幕截图:

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

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

10-10 21:44
查看更多