I am using AE.NET to get mail from Gmail using IMAP. I am able to get the messages, however when I try iterate over the message attachments there are none. Returning message.Value.Attachments.Count() gives me 0.
using (var imap = new AE.Net.Mail.ImapClient("imap.gmail.com", mailAccount.UserName, mailAccount.Password, AE.Net.Mail.ImapClient.AuthMethods.Login, 993, true))
{
//Get all new messages
var msgs = imap.SearchMessages(
SearchCondition.Unseen()
);
string ret = "";
foreach (var message in msgs)
{
foreach (var attachment in message.Value.Attachments)
{
//Save the attachment
}
}
}
As I said I have logged the attachment count along with the subject of the mail and am sure that the mails are being retrieved however they have no attachments, which is not true because I can see the attachments in Gmail.
You are doing imap.SearchMessages(SearchCondition.Unseen())
only, but this method loads only the headers of the mail. You need to use the code below by message's ids that you got in the SearchMessages
method:
List<string> ids = new List<string>();
List<AE.Net.Mail.MailMessage> mails = new List<AE.Net.Mail.MailMessage>();
using (var imap = new AE.Net.Mail.ImapClient("imap.gmail.com", mailAccount.UserName, mailAccount.Password, AE.Net.Mail.ImapClient.AuthMethods.Login, 993, true))
{
var msgs = imap.SearchMessages(SearchCondition.Unseen());
for (int i = 0; i < msgs.Length; i++) {
string msgId = msgs[i].Uid;
ids.Add(msgId);
}
foreach (string id in ids)
{
mails.Add(imap.GetMessage(id, headersonly: false));
}
}
And then use:
foreach(var msg in mails)
{
foreach (var att in msg.Attachments)
{
string fName;
fName = att.Filename;
}
}