Convert MIME tree to MailMessage

让人想犯罪 __ 提交于 2019-12-19 09:26:29

问题


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 AlternateViews, LinkedResources, and Attachments?

EDIT: That will work with all mail clients (both sending and receiving)


回答1:


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).

  1. Check the following conditions for the body:

    1. If the 1st part is HTML,set it to the body of the message

    2. 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.

    3. 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.

  2. Loop through the remainder of the parts. Add everything else as an attachment, except

    1. images that have a content-id header set, or

    2. 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.




回答2:


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).




回答3:


Right now, I'm copying anything with a Content-Disposition which isn't inline, or with a MIME category of anything other than Text or Image, to an Attachment, anything inline, or anything with a MIME category of Image to a LinkedResource on the HTML view or the last view, and anything else as an AlternateView. (I haven't tested this yet)




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/1000954/convert-mime-tree-to-mailmessage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!