3

I am downloading messages over IMAP using MailFolder.Fetch and then getting the relevant body parts with MailFolder.GetBodyPart.

Is it possible to get the raw representation of the MIME message in addition to the processed information?

Code:

var query = CreateSearchQuery(startDate, endDate);
await mailFolder.OpenAsync(FolderAccess.ReadOnly);
var results = await mailFolder.SearchAsync(SearchOptions.All, query);
var messageSummaries = await mailFolder.FetchAsync(results.UniqueIds, DownloadFlags);

Later I download the message by running over the parts, finding the html and the plain parts and downloading them as shown in this question (only I download the parts that are not IsAttachment==true).

Community
  • 1
  • 1
Ziv
  • 2,755
  • 5
  • 30
  • 42

2 Answers2

3

If you download only individual MIME parts, it won't be possible to get the raw representation of the full message (unless you download all of the parts and reconstruct the message), but you can get the raw representation of each of the MIME parts that you download by using the WriteTo() method that exists on each MimeEntity that you get back from MailFolder.GetBodyPart().

If you want the raw representation of the full message, then the easiest way to get that is to use MailFolder.GetMessage() and then invoke the WriteTo() method on the MimeMessage that you get back or you can use MailFolder.GetStream() in the following way:

var raw = folder.GetStream (uid, string.Empty);

I would suggest that if you know that you'll want the full raw message, it's best to avoid downloading individual parts first as it'll just be a waste of bandwidth. If you decide to use GetStream(), you can feed the stream to MimeMessage.Load() and get back a parsed MimeMessage object.

Note: If you use the WriteTo() method, don't forget to seek back to 0 in the stream if you want to read the raw message or body part data.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Thanks, does GetMessage always download the entire message with the attachments? Is it possible to download it without the attachments and still use WriteTo? – Ziv Oct 15 '15 at 15:36
  • Also, is it possible to get the data behind a Fetch call? That gets only the headers? – Ziv Oct 15 '15 at 15:37
  • GetMessage() will download the entire message including attachments. The only way to get the message without the attachments is to download the individual body parts as you are currently doing, however, that means you won't be able to have the full raw representation of the message because the full raw message would include the attachments. – jstedfast Oct 15 '15 at 15:51
  • You can request individual headers in a `Fetch()` call, but you can't easily request *all* headers that way. I would recommend using `GetStream (uid, "HEADER")` to get the raw message headers which you can then parse using `HeaderList.Load()` if you want the parsed version. – jstedfast Oct 15 '15 at 15:54
  • Thank you very much, I will simply download the entire message and use the EncodeTo. Thank you very much for this great, high quality library. – Ziv Oct 18 '15 at 09:32
0

In the end, what I need is not necessarily the raw data of a specific message but the evidence that the data I got from the server is the data presented to the user, which actually WriteTo isn't doing that exactly since it's converting the data loaded into the C# objects back to a string.

I was going to open up the library and redirect the ImapStream to a new type that duplicates the data to another stream, but luckily Jeff has already thought of that and implemented the option to inject a ProtocolLogger which already does that.

Hopefully that helps someone in the future.

Ziv
  • 2,755
  • 5
  • 30
  • 42