使用 MailMessage 的 LinkedResources 嵌入文档

发布于 2024-12-27 06:52:03 字数 1239 浏览 2 评论 0原文

我使用以下代码将图像嵌入到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许你一世情深 2025-01-03 06:52:03

尝试使用 cid: 而不是 cdi:。这是我想到的一个错误。

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

无所谓啦 2025-01-03 06:52:03

尝试使用

href="cid:companylogo 

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

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

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

希望它有帮助!

;)

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!

;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文