如何创建带有嵌入图像的 HTML 电子邮件并使用 .net (c#) 在默认邮件客户端中显示它?
如果您查看 Windows 中的 Snip 工具,可以选择将片段作为嵌入附件发送到默认邮件客户端(我猜是 html 电子邮件)。我正是需要那个。
目前我正在使用简单的 MAPI 来附加图像,但不允许嵌入。
一项关键要求是应创建它,然后将其显示在默认邮件客户端中。我认为我不能使用 System.Net.Mail 来实现这一点。或者我可以吗?
它是一个使用 C# 的 WPF 应用程序。
编辑:请在回答之前仔细阅读问题。我对此并不陌生。我知道如何在非托管代码中执行此操作,而无需求助于黑客。我需要一个托管的等效项。我不是寻找黑客手段,并且很乐意在必要时花大量时间寻找正确的解决方案。
If you look at the Snip tool in windows there is an option to send the snip as an embedded attachment in the default mail client (I guess as an html email). I need exactly that.
At the moment I am using simple MAPI to attach the image but that does not allow embedding.
One key requirement is that it should be created and then shown in the default mail client. I don't think I can use System.Net.Mail for that. Or can I?
Its a WPF application using c#.
EDIT: Please read the question carefully before answering. I am not new to this. I know how to do this in unmanaged code without resorting to hacks. I need a managed equivalent. I am not looking for hacks and am happy to spend significant time on the correct solution if necessary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为在 mailto 中您可以嵌入图像
请参阅 RFC 2368。它说 mailto 的目的仅适用于短文本消息,不适用于 MIME 正文
I do not think that in mailto you can embed images
Please refer to the RFC 2368. It says that mailto purpose is only for short text messages and not for MIME bodies
将 Process.Start 与 ShellExecute 选项结合使用:
我不确定路径周围的引号。
编辑
只是想到了另一个应该可行的解决方案。
Process.Start
打开EML文件。它应该在默认电子邮件客户端中打开它。
Use Process.Start with the ShellExecute option on this:
I'm unsure of the quotes around the path.
Edit
Just thought of another solution which should work.
Process.Start
the open the EML file.It should open it in the default email client.
我认为您可以使用命名空间 System.Net.Mail 中的类来完成这项工作。这需要做相当多的工作。我将把它作为读者的练习。
根据 RFC2111,嵌入图像的 HTML 电子邮件仅存储 HTML 电子邮件正文和图像两个不同 MIME 内容中的附件。
关键点是,在 HTML 电子邮件正文中,您不使用常规 URL 路径,而是输入“cid:xxxxx”,其中 xxxxx 是您的内容 ID。有关 Content ID 的更多详细信息,请查看 RFC2111。从我在 System.Net.Mail 命名空间中看到的情况来看,它允许您创建 IsBodyHtml 属性设置为 true 的 MailMessage。然后,您需要做的下一件事是将附件添加到此 MailMessage。附件当然是您的嵌入图像。然后,您需要记住设置附件的ContentID。因此,只要确保您的 HTML 电子邮件引用正确的 ContentID,一切就应该可以正常工作。
同样,我自己没有验证,但我快速检查了一些嵌入图像的电子邮件。我确实看到了以 cid:xxxx 作为图像源的电子邮件,我的 Outlook 客户端确实可以正常打开它。
I think you can use classes in namespace System.Net.Mail to do this job. This requires quite some work. I will leave it as reader's exercise.
According to RFC2111, a HTML email with embedded image is just storing HTML email body and the image attachment in two different MIME content.
The key point is that in your HTML email body, instead of using a regular URL path, you put in "cid:xxxxx" where xxxxx is your Content ID. Check RFC2111 for more details about Content ID. From what I see in the System.Net.Mail namespace, it allows you to create a MailMessage with IsBodyHtml property set to true. Then, the next thing you need to do is to add Attachment to this MailMessage. The Attachment is of course your embedded image. Then, you need to remember to set the ContentID of the Attachment. So, just to make sure your HTML email is referencing the correct ContentID, the things should work.
Again, I didn't verify it myself but I did a quick check on some of my emails with embedded image in it. I did see the email with cid:xxxx as the image source and my Outlook client can really open it fine.