使用C#Windows应用程序在电子邮件正文中(内联)添加多个图像 [英] Add multiple images in the email body (inline)using c# windows application
问题描述
我搜索了几次,找到了解决方案,但是它们都只支持一张图像。最后我使用了这段代码。
但是问题是,如果html包含多个图像,则仅在主体中显示一个图像,而其他图像将作为附件出现。
I searched for this several times and found solutions,but all supports only one image.Finally I used this code. But the problem is,if the html contain more than one image only one image is shown in the body and the others will come as attachment.
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);
请帮助我解决此问题,如何显示电子邮件正文中的所有图像?...
Please help me to resolve this,How to show all images inside email body?...
推荐答案
您可以将图像附加到邮件中,然后放置 img
标记并使用< a href = https://msdn.microsoft.com/zh-cn/library/microsoft.exchange.webservices.data.attachment.contentid(v=exchg.80).aspx rel = nofollow noreferrer> <$附件c $ c> ContentId 作为 src
的方式是:
You can attach images to mail and then put img
tag and useContentId
of attachment as src
this way:
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#Windows应用程序在电子邮件正文中(内联)添加多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!