将 MIME 树转换为 MailMessage
我正在编写一个处理和转发电子邮件的 C# 程序。 我有一个 POP3 库和一个 MIME 解析器,我需要将 MIME 树复制到 System.Net.Mail.MailMessage 中。
将不同 MIME 部分映射到 AlternateView
、LinkedResource
和 Attachment
的最佳方法是什么?
编辑:这将适用于所有邮件客户端(发送和接收)
I'm writing a a C# program that processes and forwards email messages. I have a POP3 library and a MIME parser, and I need to copy the MIME tree into a System.Net.Mail.MailMessage
.
What is the best way to map different MIME parts to AlternateView
s, LinkedResource
s, and Attachment
s?
EDIT: That will work with all mail clients (both sending and receiving)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可能不是最好的方法,但我会尝试获取“原始”电子邮件,然后将“to”替换为新电子邮件
Might not be the best way, but i'd try to get the 'raw' email and just replace to the to with the new e-mail
嗯,您绝对需要使用 System.Net.Mail.MailMessage 吗?
System.Net.Mail.MailMessage 仅提供 MIME 所提供功能的一小部分。 因此,如果您的 MIME 解析器旨在支持所有/大多数标准功能,那么将 MIME 消息复制到 MailMessage 的目标往好里说是困难的,往坏了说是不可能的。 提供 POP3 访问的图书馆是否也提供 SMTP 访问? 如果是这样,我会保留 System.Net.Mail.MailMessage 并使用第三方库提供的任何内容。
Hmmmm, do you absolutely need to use System.Net.Mail.MailMessage?
System.Net.Mail.MailMessage offers only a very small subset of what MIME offers. So, if your MIME parser aims to support all/most standard features then your goal of copying a MIME message into MailMessage will be difficult at best and impossible at worst. Doesn't the library providing POP3 access also provide SMTP access? If so, I'd leave System.Net.Mail.MailMessage alone and go with whatever the thrid party library provides.
从 10,000 英尺的高度来看,这就是我要做的事情。
将你的哑剧部分压平成一棵树。 确保每个部分包含 1 个且仅 1 个部分(不是像多部分/相关的父部分或类似的东西)。
检查身体的以下状况:
如果第一部分是HTML,则将其设置为消息正文
如果第一部分是纯文本,而第二部分不是 html,则将纯文本部分设置为消息正文。
如果第一部分是纯文本,第二部分是 html,则创建 2 个替代视图。
***这假设这些部分都没有 Content-Disposition:attachment 标头。
循环遍历其余部分。 将其他所有内容添加为附件,除了
设置了 content-id 标头的图像,或
设置了内容位置标头的图像。
如果这些标头之一存在,那么我会将这些图像添加为 LinkedResource(仅当实际上存在 HTML 正文部分时)。
这应该可以帮助您入门,并涵盖大约 99% 的普通电子邮件。
From a 10,000ft overview, here is what I would do.
Flatten your mime parts into a tree. Make sure each part contains 1, and only 1 part (not a parent like a multipart/related, or something like that).
Check the following conditions for the body:
If the 1st part is HTML,set it to the body of the message
If the 1st part is plain text, and the 2nd part is not html, set the plain text part to the body of the message.
If the first part is plain, and the 2nd part is html, create 2 alternative views.
***This assumes none of these parts has a Content-Disposition:attachment header.
Loop through the remainder of the parts. Add everything else as an attachment, except
images that have a content-id header set, or
images that have a content-location header set.
If one of those headers exist, then I would add those images in as a LinkedResource (only if there is actually a HTML body part).
That should get you started, and cover about 99% of the normal email out there.
将多部分/替代部分中包含的任何文本部分(文本/纯文本、文本/html 等)映射到 AlternateView。 还将遇到的第一个文本部分映射到 AlternateView,无论其父类型如何,以满足消息仅包含单个文本部分的情况。
将剩余部分映射到 Attachment 或 LinkedResource,具体取决于 Content-Disposition 标头。
将具有附件内容处置的那些部分映射到附件。
将具有内联 Content-Disposition 或没有 Content-Disposition 标头的那些部分映射到 LinkedResource。 最后一步可以通过检查 Content-ID 是否与特定文本部分引用的 Content-ID 匹配来进行巧妙处理,但出于实际目的,可以假设以这种方式创建的所有 LinkedResources 都属于第一个 text/html AlternateView (或者最后创建的 AlternateView,如果没有 text/html 类型的 AlternateView)。
Map any text part (text/plain, text/html etc.) that is contained within a multipart/alternative part to an AlternateView. Also map the first text part encountered to an AlternateView, regardless of its parent type, to cater for the case of the message only consisting of a single text part.
Map the remaining parts to an Attachment or a LinkedResource, depending on the Content-Disposition header.
Map those parts with a Content-Disposition of attachment, to an Attachment.
Map those parts with a Content-Disposition of inline, or no Content-Disposition header, to a LinkedResource. This last step could be finessed by checking that the Content-ID matched a Content-ID referred to from a particular text part, but for practical purposes, it could be assumed that all LinkedResources created in this way belong to the first text/html AlternateView (or the last AlternateView created, if there is no AlternateView of type text/html).
现在,我正在将带有非
内联
的Content-Disposition
的任何内容,或者带有除文本或图像之外的任何 MIME 类别的内容复制到附件
、任何内联
或任何具有Image
MIME类别的内容到HTML视图或最后一个视图上的LinkedResource
,以及其他作为AlternateView
的内容。 (我还没有测试过这个)Right now, I'm copying anything with a
Content-Disposition
which isn'tinline
, or with a MIME category of anything other than Text or Image, to anAttachment
, anythinginline
, or anything with a MIME category ofImage
to aLinkedResource
on the HTML view or the last view, and anything else as anAlternateView
. (I haven't tested this yet)