Embed Documents using LinkedResources for MailMessage

1.2k views Asked by At

I'm using the following code to embed images into my MailMessage. What I'm trying to do is embed documents (pdf or docx) into the email.

I've tried hyperlink with a link to href="cdi:myDoc.pdf" but that doesn't work. I've also tried using MailMessage.Attachments.Add() but adds the documents in the attachments section instead of embeding the document in the message.

Anyone how to embed a document in the mailmessage? I know Outlook is able to place the attachments in the body of the message but I can't figure how to do it through mailMessage.

Thanks Susan

Sub MultiPartMime()
Dim mail As New MailMessage()

mail.From = New MailAddress("[email protected]")
mail.To.Add("[email protected]")

mail.Subject = "This is an email"

Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString("<b>this is bold text, and viewable by <img src=""cdi:companylogo""> those mail clients that support html</b>", Nothing, "text/html")

LinkedResource logo = new LinkedResource( "c:\temp\logo.gif" )
logo.ContentId = "companylogo"
htmlView.LinkedResources.Add(logo)


mail.AlternateViews.Add(htmlView)


'send the message
Dim smtp As New SmtpClient("127.0.0.1") 'specify the mail server address
smtp.Send(mail)
End Sub 'MultiPartMime
2

There are 2 answers

0
Haga On

try to use

href="cid:companylogo 

(with "cid" instead of "cdi" Like Jakob Mygind suggested) and set it to the contentId that you specified for the LinkedResource.

Also when setting the path to the file, it is good to use the HostingEnvironment.MapPath() method (which is the same of Url.Content() of web projects. It would go with something something like:

LinkedResource logo = new LinkedResource(HostingEnvironment.MapPath("c:\temp\logo.gif"));

Hope it helps!

;)

0
Jakob Mygind On

Try using cid: instead of cdi:. That is one error that comes to mind.