This example demonstrates how download only .zip attachments and skip over all other attachments of a mail message.
Downloading ZIP attachments (while skipping any other attachments)
CopyC#
using System;
using S22.Imap;
namespace Test {
class Program {
static void Main(string[] args)
{
using (ImapClient Client = new ImapClient("imap.gmail.com", 993, "username",
"password", AuthMethod.Login, true))
{
// This returns all messages sent since August 23rd 2012.
IEnumerable<uint> uids = Client.Search(
SearchCondition.SentSince( new DateTime(2012, 8, 23) )
);
// The expression will be evaluated for every MIME part
// of every mail message in the uids collection.
IEnumerable<MailMessage> messages = Client.GetMessages(uids,
(Bodypart part) => {
// We're only interested in attachments.
if(part.Disposition.Type == ContentDispositionType.Attachment)
{
// Zip files have a content-type of application/zip.
if(part.Type == ContentType.Application &&
part.Subtype.toLower() == "zip")
{
return true;
}
else
{
// Skip this attachment, it's not a zip archive.
return false;
}
}
// Fetch MIME part and include it in the returned MailMessage instance.
return true;
}
);
}
}
}
}