具有多个嵌入图像的VB.NET电子邮件 [英] VB.NET Email with multiple embedded Images
问题描述
请有人给我一些关于如何发送带有MULTIPLE嵌入图像的电子邮件的指示。
Please can someone give me some pointers on how to send an email with MULTIPLE embedded images.
我可以发一个基本的电子邮件,我也可以发送一封电子邮件使用AlternateView的单个嵌入式图像
I can send a basic email and I can also send an email with an single embedded image using AlternateView,
在bodyText中,我有XElement我有:< img src ='cid:SCREENSHOT'/>
In bodyText as XElement I have : <img src='cid:SCREENSHOT'/>
然后我添加这样的替代视图:
Then I add the alternative view like this:
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)`
我只是无法弄清楚如何获得多个
I just cant work out how to get multiple images in there.
非常感谢
Richard。 p>
Richard.
推荐答案
它涉及2个循环。
1循环在创建点身体。
1 loop at the point of creatng the body.
从BodyText调用这个:<%= CreateImages(imagePaths,hasCustImage)%>
Call this from BodyText using: <%= 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
创建替代视图的另一个循环:
Another loop to create the Alternative views:
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)
这篇关于具有多个嵌入图像的VB.NET电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!