使用 c# windows 应用程序在电子邮件正文中添加多个图像(内联)
Posted
技术标签:
【中文标题】使用 c# windows 应用程序在电子邮件正文中添加多个图像(内联)【英文标题】:Add multiple images in the email body (inline)using c# windows application 【发布时间】:2016-02-13 09:55:36 【问题描述】:我搜索了几次并找到了解决方案,但都只支持一个图像。最后我使用了这个代码。 但问题是,如果 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);
请帮我解决这个问题,如何在电子邮件正文中显示所有图像?...
【问题讨论】:
【参考方案1】:试试这个代码。这将为 excel 、 csv 、 pdf 以及内联附件生成附件,即在电子邮件正文中生成 png 格式的图像。 对于 png 格式,多个图像将通过 GetEmbeddedImage 方法逐个嵌入内联。您可以根据自己的要求对其进行自定义。
SmtpClient smtpServer = new SmtpClient(smtpServerName);
smtpServer.Port = 25;
smtpServer.Credentials = new System.Net.NetworkCredential(userName, password);
//smtpServer.EnableSsl = true;
MailMessage smtpEmail = new MailMessage();
string messageBodyImage = @"<img width=1200 id=""MyContent"" src=""cid:0"">";
toAddressList = toAddress.Split(';');
foreach (string toEmail in toAddressList)
smtpEmail.To.Add(toEmail);
smtpEmail.From = new MailAddress(fromAddress);
smtpEmail.Body = messageBody;
smtpEmail.Subject = subject;
foreach (string format in fileExtension)
switch (format)
case "PNG":
smtpEmail.IsBodyHtml = true;
smtpEmail.AlternateViews.Add(GetEmbeddedImage(reportByteStream, messageBodyImage, format));
break;
case "CSV":
smtpEmail.Attachments.Add(new System.Net.Mail.Attachment(new MemoryStream(myStream[format][0]), "MyReport." + format, "text/csv"));
break;
case "XLS":
smtpEmail.Attachments.Add(new System.Net.Mail.Attachment(new MemoryStream(myStream[format][0]), "MyReport." + format, "application/vnd.ms-excel"));
break;
default: // For PDF
smtpEmail.Attachments.Add(new System.Net.Mail.Attachment(new MemoryStream(myStream[format][0]), "MyReport." + format, MediaTypeNames.Application.Pdf));
break;
内嵌多张图片的方法。
private AlternateView GetEmbeddedImage(Dictionary<string, Byte[][]> streamAttachment, string msgTemplate, string fileFormat)
LinkedResource imageFile = null;
AlternateView alternateView = null;
string msgBody = string.Empty;
try
List<LinkedResource> imageFiles = new List<LinkedResource>();
for (int page = 0; page < streamAttachment[fileFormat].Length; page++)
imageFile = new LinkedResource(new MemoryStream(streamAttachment[fileFormat][page]));
imageFile.ContentId = Guid.NewGuid().ToString();
msgBody = msgBody + "<BR/>" + string.Format(msgTemplate, imageFile.ContentId);
imageFiles.Add(imageFile);
alternateView = AlternateView.CreateAlternateViewFromString(msgBody, null, MediaTypeNames.Text.Html);
imageFiles.ForEach(img => alternateView.LinkedResources.Add(img));
catch (Exception Ex)
return alternateView;
【讨论】:
【参考方案2】:您可以将图片附加到邮件中,然后将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);
这是截图:
【讨论】:
这将有助于只添加一个图像,我的问题是如何添加多个图像 您可以简单地添加多个喜欢的资源。 我在上面的代码中显示,但问题是它只显示第一张图片,其他的作为附件。 @sayana 检查编辑,它也适用于多个图像,测试,只需将其复制并粘贴到您的代码中,并使用正确的地址和凭据并查看结果。 谢谢我看到了,但是如何循环写它。我有很多图像。以上是关于使用 c# windows 应用程序在电子邮件正文中添加多个图像(内联)的主要内容,如果未能解决你的问题,请参考以下文章
如何使用asp.net c#将HTML格式的电子邮件正文传递给javascript函数