I'm using the code from here to download attachments from mail using MailKit. In the foreach loop where attachments are retrieved, it is always returning empty. As it is empty it is not entering the foreach loop. Please correct me if i'm doing anything wrong.
var messages = client.Inbox.Fetch(0, -1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId);
int unnamed = 0;
foreach (var message in messages)
{
var multipart = message.Body as BodyPartMultipart;
var basic = message.Body as BodyPartBasic;
if (multipart != null)
{
//Here the attachments is always empty even though my mail has email with attachements
var attachments = multipart.BodyParts.OfType<BodyPartBasic>().Where(x => x.IsAttachment);
foreach (var attachment in attachments)
{
var mime = (MimePart)client.Inbox.GetBodyPart(message.UniqueId.Value, attachment);
var fileName = mime.FileName;
if (string.IsNullOrEmpty(fileName))
fileName = string.Format("unnamed-{0}", ++unnamed);
using (var stream = File.Create(fileName))
mime.ContentObject.DecodeTo(stream);
}
}
else if (basic != null && basic.IsAttachment)
{
var mime = (MimePart)client.Inbox.GetBodyPart(message.UniqueId.Value, basic);
var fileName = mime.FileName;
if (string.IsNullOrEmpty(fileName))
fileName = string.Format("unnamed-{0}", ++unnamed);
using (var stream = File.Create(fileName))
mime.ContentObject.DecodeTo(stream);
}
}