请有人就如何发送带有多个嵌入式图像的电子邮件给我一些指示。
我可以发送基本电子邮件,也可以使用AlternateView发送包含单个嵌入式图像的电子邮件,
在bodyText中作为XElement,我有:<img src='cid:SCREENSHOT'/>
然后我添加了这样的替代视图:
Dim htmlContent As AlternateView = AlternateView.CreateAlternateViewFromString(bodyText.ToString(), Nothing, mediaType)
If (IO.File.Exists(screenshotPath)) Then
Dim screenshot As New LinkedResource(screenshotPath)
screenshot.ContentId = "SCREENSHOT"
htmlContent.LinkedResources.Add(screenshot)
End If
msg.AlternateViews.Add(htmlContent)`
我无法弄清楚如何在那里获得多张图片。
非常感谢
理查德。
答案 0 :(得分:3)
只需添加多个链接资源即可。澄清:
在您的身体中,添加多个图像参考:
<img src='cid:SCREENSHOT1'/>
<img src='cid:SCREENSHOT2'/>
在您的代码中,添加多个图片:
If (IO.File.Exists(screenshotPath1)) Then
Dim screenshot As New LinkedResource(screenshotPath1)
screenshot.ContentId = "SCREENSHOT1"
htmlContent.LinkedResources.Add(screenshot)
End If
If (IO.File.Exists(screenshotPath2)) Then
Dim screenshot As New LinkedResource(screenshotPath2)
screenshot.ContentId = "SCREENSHOT2"
htmlContent.LinkedResources.Add(screenshot)
End If
(这似乎有点过于简单。如果我误解了你的问题,请说出来。)
答案 1 :(得分:2)
它涉及2个循环。
创建正文时的1个循环。使用:<%= CreateImages(imagePaths, hasCustImage) %>
Private Shared Function CreateImages(ByVal imagePaths As IList(Of String), ByVal hasCustImage As Boolean) As XElement
Dim images As XElement = <span></span>
Dim temp As XElement = Nothing
For i As Integer = 0 To imagePaths.Count - 1
Dim img As String = String.Format("cid:ItemImage{0}", i + 1)
If ((i = (imagePaths.Count - 1)) And (hasCustImage)) Then
temp = _
<p style="font-family: Arial; font-size: 10pt">
Customer:<br/>
<img src=<%= img %>/>
</p>
Else
temp = _
<p style="font-family: Arial; font-size: 10pt">
<img src=<%= img %>/>
</p>
End If
images.Add(temp)
Next
Return images
End Function
另一个创建备用视图的循环:
msg.Body = bodyText.ToString()
Dim htmlContent As AlternateView = AlternateView.CreateAlternateViewFromString(bodyText.ToString(), Nothing, mediaType)
For i As Integer = 0 To imagePaths.Count - 1
If (IO.File.Exists(imagePaths(i))) Then
Dim itemImage As New LinkedResource(imagePaths(i))
itemImage.ContentId = String.Format("ItemImage{0}", i + 1)
htmlContent.LinkedResources.Add(itemImage)
End If
Next
msg.AlternateViews.Add(htmlContent)
Return Utils.Send(msg)
答案 2 :(得分:0)
我之前在C#中做过这个。从Bitmap对象开始,并使用LinkedResource ID将它们链接到我的HTML源代码中。然后我将包含HTML源代码的AlternateView作为电子邮件正文附加到我的MailMessage。
String id1= "yourid";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<img src=cid:" + id1 + ">",null, "text/html"); // add to html as required
Bitmap rc_img1 = new Bitmap(rc_bar.GetBitmap());
MemoryStream ms = new MemoryStream();
rc_img1.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
LinkedResource myImg1 = new LinkedResource(ms);
myImg1.ContentId = id1;
htmlView.LinkedResources.Add(myImg1);
mail.AlternateViews.Add(htmlView);