使用 MailMessage 的 LinkedResources 嵌入文档
我使用以下代码将图像嵌入到我的 MailMessage 中。我想做的是将文档(pdf 或 docx)嵌入到电子邮件中。
我尝试过使用指向 href="cdi:myDoc.pdf" 的链接的超链接,但这不起作用。我还尝试过使用 MailMessage.Attachments.Add() 但将文档添加到附件部分,而不是将文档嵌入到消息中。
有人如何在邮件消息中嵌入文档吗?我知道 Outlook 能够将附件放置在邮件正文中,但我不知道如何通过 mailMessage 来做到这一点。
谢谢苏珊
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
cid:
而不是cdi:
。这是我想到的一个错误。Try using
cid:
instead ofcdi:
. That is one error that comes to mind.尝试使用
(使用“cid”而不是“cdi”,就像 Jakob Mygind 建议的那样)并将其设置为您为 LinkedResource 指定的 contentId。
另外,在设置文件路径时,最好使用 HostingEnvironment.MapPath() 方法(与 Web 项目的 Url.Content() 相同。它会带有类似以下内容:
希望它有帮助!
;)
try to use
(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:
Hope it helps!
;)