可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Please can someone give me some pointers on how to send an email with MULTIPLE embedded images.
I can send a basic email and I can also send an email with an single embedded image using AlternateView,
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.
Many Thanks
Richard.
回答1:
Just add multiple linked resources. To clarify:
In your body, add multiple image references:
<img src='cid:SCREENSHOT1'/> <img src='cid:SCREENSHOT2'/>
In your code, add multiple images:
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
(This seems a bit too simple. If I misunderstood your question, please say so.)
回答2:
It involves 2 loops.
1 loop at the point of creatng the body.
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)
回答3:
I've done this before in C#. Started with Bitmap objects and linked them into my HTML source with LinkedResource IDs. Then I attached the AlternateView containing the HTML source as the e-mail body to my 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);