使用LinkedResources为MailMessage嵌入文档

时间:2012-01-13 06:50:06

标签: attachment smtpclient mailmessage alternateview

我正在使用以下代码将图像嵌入到我的MailMessage中。我想要做的是将文档(pdf或docx)嵌入到电子邮件中。

我尝试过带有href =“cdi:myDoc.pdf”链接的超链接,但这不起作用。我也尝试过使用MailMessage.Attachments.Add(),但是在附件部分添加了文档,而不是在邮件中嵌入文档。

有人如何在mailmessage中嵌入文档?我知道Outlook能够将附件放在邮件正文中,但我无法通过mailMessage来计算它是如何做到的。

谢谢苏珊

Sub MultiPartMime()
Dim mail As New MailMessage()

mail.From = New MailAddress("me@mycompany.com")
mail.To.Add("you@yourcompany.com")

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 个答案:

答案 0 :(得分:2)

尝试使用cid:代替cdi:。这是一个想到的错误。

答案 1 :(得分:1)

尝试使用

href="cid:companylogo 

(使用“ cid ”代替“cdi”,就像Jakob Mygind建议的那样)并将其设置为您为LinkedResource指定的contentId。

同样在设置文件的路径时,最好使用HostingEnvironment.MapPath()方法(与Web项目的Url.Content()相同。它会带有类似的东西:

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

希望它有所帮助!