This project is read-only.

ignoring attachments

Topics: Help requests
Jul 3, 2014 at 4:46 PM
Is there a way to ignore attachments when downloading messages? I only need to read the message to determine how to sort it.

Thanks,
~S
Coordinator
Jul 4, 2014 at 10:03 AM
Hi slsumner,

the library is quite flexible when it comes to limiting the information you retrieve. You can define which data is downloading by changing the client.Behavior.MessageFetchMode property. For more details take a look at the documentation. Another thing you can do is limiting the headers which will be downloaded, for this change the client.Behavior.RequestedHeaders property.

A quickstart example:
var client = new ImapClient("imap.gmail.com", true);

// Set the client to download only flags, headers and message bodies (no attachments)
client.Behavior.MessageFetchMode = MessageFetchMode.Flags | MessageFetchMode.Headers | MessageFetchMode.Body;

// Limit the headers to be fetched
client.Behavior.RequestedHeaders = new[] {
      MessageHeader.From,
      MessageHeader.To,
      MessageHeader.Subject,
      MessageHeader.Date,
      MessageHeader.MessageId,
      MessageHeader.ContentType
};

// .. connect, sign in

var messages = client.Folders.Inbox.Search();

// In case you need to fetch additional data later, simply call the Message.Download method
// and change the fetch mode. In case you changed the client.Behavior.RequestedHeaders also,
// set the second argument to true
foreach(var msg in messages)
{
      msg.Download(client.Behavior.MessageFetchMode.Full, true);
}

// In case you only need to download the missing attachments:

foreach(var file in msg.Attachements)
{
       file.Download();
}
Greets,

Pavel
Jul 7, 2014 at 2:51 PM
Thanks! Things are running much faster now. I do still receive an error on some emails that causes an out of memory exception in the BodyStructureParser while in Outlook. (line 327). I cannot seem to pinpoint what is causing these particular emails to hang, while others are just fine aside from that they all seem to have 1 or more attachments on them and that they are bounced messages. I think it's happening while it's reading the headers. Any thoughts?