2011-09-20 58 views
2

請有人給我一些關於如何發送帶有MULTIPLE嵌入式圖像的電子郵件的指針。帶有多個嵌入式圖像的VB.NET電子郵件

我可以發送一個基本電子郵件,我還可以使用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)` 

我只是不能解決如何獲得多個圖像在那裏。

很多謝謝

Richard。

回答

2

它涉及2個循環。

1循環在身體的creatng點。使用

調用此從BODYTEXT:<%= 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) 
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); 
+0

嗨。這不僅僅是添加一個圖像嗎? –

+0

是的,但只要你有不同的ID,你可以像這樣添加儘可能多的。 – Brissles

+0

@Brissles:那我真的不明白你的答案中的重點。正如理查德在他的問題中所寫的那樣:「我可以發送一封基本的電子郵件,並且我還可以使用AlternateView'發送帶有單個嵌入式圖像的電子郵件。他已經知道如何做*那*。 – Heinzi

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 

(這似乎有點太簡單了。如果我誤解了你問題請說清楚。)

+0

這很簡單,但圖像的數量不固定,範圍可以從1到10. –

+0

@Richard:然後做一個循環。你需要幫助嗎? – Heinzi

相關問題