Create Mail message object
Build HTML string for images
Add HTML string in AlternateView
Add images as LinkedResources in AlternateView
Add AlternateView to Mail message.
Below code will demonstrate the steps:
Build HTML string for images
Add HTML string in AlternateView
Add images as LinkedResources in AlternateView
Add AlternateView to Mail message.
Below code will demonstrate the steps:
Dim fileNames As String() = System.IO.Directory.GetFiles("C:\imageFolder\", "*.png")
Dim mailMsg As MailMessage = New MailMessage()
Dim imageHTML as String = String.Empty
Dim htmlView As AlternateView
For i As Int16 = 0 To fileNames.Count - 1
imageHTML = imageHTML & "<table style=""padding:0px;margin:0px;border:2 solid black;""><tr><td valign=""top"" align=""left""><img width=720 height=930 src=cid:" & System.IO.Path.GetFileName(fileNames(i)) & "></td></tr></table>"
Next
htmlView = AlternateView.CreateAlternateViewFromString(imageHTML, Nothing, "text/html")
For i As Int16 = 0 To fileNames.Count - 1
' Linked source take physical image path to embed image.
Dim imagelink As LinkedResource = New LinkedResource(fileNames(i))
imagelink.ContentId = System.IO.Path.GetFileName(fileNames(i)) 'imgFile.Name
imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64
htmlView.LinkedResources.Add(imagelink)
Next
mailMsg.AlternateViews.Add(htmlView)
No comments:
Post a Comment