如何创建带有嵌入图像的 HTML 电子邮件并使用 .net (c#) 在默认邮件客户端中显示它?

发布于 2024-10-08 21:09:59 字数 338 浏览 6 评论 0原文

如果您查看 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 技术交流群。

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

发布评论

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

评论(3

终弃我 2024-10-15 21:09:59

我不认为在 mailto 中您可以嵌入图像
请参阅 RFC 2368。它说 mailto 的目的仅适用于短文本消息,不适用于 MIME 正文

   The special hname "body" indicates that the associated hvalue is the
   body of the message. The "body" hname should contain the content for
   the first text/plain body part of the message. The mailto URL is
   primarily intended for generation of short text messages that are
   actually the content of automatic processing (such as "subscribe"
   messages for mailing lists), not general MIME bodies.

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

   The special hname "body" indicates that the associated hvalue is the
   body of the message. The "body" hname should contain the content for
   the first text/plain body part of the message. The mailto URL is
   primarily intended for generation of short text messages that are
   actually the content of automatic processing (such as "subscribe"
   messages for mailing lists), not general MIME bodies.
小猫一只 2024-10-15 21:09:59

将 Process.Start 与 ShellExecute 选项结合使用:

mailto:[email protected]?subject=your+file&body=see attachment&attachment="C:\path\to\file.txt"

我不确定路径周围的引号。

编辑

只是想到了另一个应该可行的解决方案。

  1. 使用 MailMessage 创建 HTML 电子邮件
  2. 保存使用此处的答案作为XXXX.eml
  3. 使用ShellExecute选项和Process.Start打开EML文件。

它应该在默认电子邮件客户端中打开它。

Use Process.Start with the ShellExecute option on this:

mailto:[email protected]?subject=your+file&body=see attachment&attachment="C:\path\to\file.txt"

I'm unsure of the quotes around the path.

Edit

Just thought of another solution which should work.

  1. Create a HTML email using MailMessage
  2. Save it using the answer here as XXXX.eml
  3. Use ShellExecute option with Process.Start the open the EML file.

It should open it in the default email client.

裂开嘴轻声笑有多痛 2024-10-15 21:09:59

我认为您可以使用命名空间 System.Net.Mail 中的类来完成这项工作。这需要做相当多的工作。我将把它作为读者的练习。

根据 RFC2111,嵌入图像的 HTML 电子邮件仅存储 HTML 电子邮件正文和图像两个不同 MIME 内容中的附件。

 From: [email protected]
 To: [email protected]
 Subject: A simple example
 Mime-Version: 1.0
 Content-Type: multipart/related; boundary="boundary-example-1";
               type=Text/HTML

 --boundary-example 1
 Content-Type: Text/HTML; charset=US-ASCII

 ... text of the HTML document, which might contain a hyperlink
 to the other body part, for example through a statement such as:
 <IMG SRC="cid:foo4*[email protected]" ALT="IETF logo">

 --boundary-example-1
 Content-ID: foo4*[email protected]
 Content-Type: IMAGE/GIF
 Content-Transfer-Encoding: BASE64

 R0lGODlhGAGgAPEAAP/////ZRaCgoAAAACH+PUNvcHlyaWdodCAoQykgMTk5
 NSBJRVRGLiBVbmF1dGhvcml6ZWQgZHVwbGljYXRpb24gcHJvaGliaXRlZC4A
 etc...

 --boundary-example-1--

关键点是,在 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.

 From: [email protected]
 To: [email protected]
 Subject: A simple example
 Mime-Version: 1.0
 Content-Type: multipart/related; boundary="boundary-example-1";
               type=Text/HTML

 --boundary-example 1
 Content-Type: Text/HTML; charset=US-ASCII

 ... text of the HTML document, which might contain a hyperlink
 to the other body part, for example through a statement such as:
 <IMG SRC="cid:foo4*[email protected]" ALT="IETF logo">

 --boundary-example-1
 Content-ID: foo4*[email protected]
 Content-Type: IMAGE/GIF
 Content-Transfer-Encoding: BASE64

 R0lGODlhGAGgAPEAAP/////ZRaCgoAAAACH+PUNvcHlyaWdodCAoQykgMTk5
 NSBJRVRGLiBVbmF1dGhvcml6ZWQgZHVwbGljYXRpb24gcHJvaGliaXRlZC4A
 etc...

 --boundary-example-1--

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.

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