Question

I am using AE.net.mail for downloading attachments from hotmail account. following is code. Messages array gets all mails according to given condition. The problem is that message.Value property for every record is null except the first record.

ImapClient ic = new ImapClient("imap-mail.outlook.com", "xxx@hotmail.com",                "xxx", ImapClient.AuthMethods.Login, 993, true))
    {
        ic.SelectMailbox("INBOX");

        Lazy<MailMessage>[] messages = ic.SearchMessages(SearchCondition.From("xxx@yahoo.com"));

        foreach (Lazy<MailMessage> message in messages)
        {
            MailMessage m = message.Value;
        }

  foreach (Attachment attachment in m.Attachments)
    {
        fileExtension = Path.GetExtension(attachment.Filename);
        attachment.Save(@"H:\Demo\" + fileName + Path.GetExtension(attachment.Filename));
    }
    }

I am not able to understand why. please guide. Thanx.

Was it helpful?

Solution

I would recommend using MailKit instead. It's a much better IMAP client implementation that actually follows the specifications.

MailKit is built on top of MimeKit which is the best/fastest/most robust MIME parser library for .NET available. It is 25x faster than OpenPOP.NET's parser, 75x faster than SharpMimeTools, 70x faster than Mail.dll, 65x faster than MIMER, and hugely faster than all of the MIME parsers included in all of the open source ImapClient implementations out there (and probably better/faster than the commercial MIME parsers as well).

Both MimeKit and MailKit are based on tokenizing stream parsers which not only means they are faster than the other libraries, it also means they can better conform to the specs and best of all, it means they do not need to read the entire response from the IMAP server into a huge string buffer before parsing the message - MailKit parses the message directly from the socket which reduces memory usage by a significant amount for large messages.

using System;
using System.Net;
using System.Threading;

using MailKit.Net.Imap;
using MailKit.Search;
using MailKit;
using MimeKit;

namespace TestClient {
    class Program
    {
        public static void Main (string[] args)
        {
            using (var client = new ImapClient ()) {
                var credentials = new NetworkCredential ("xxx@hotmail.com", "xxx");
                var uri = new Uri ("imaps://imap-mail.outlook.com");

                using (var cancel = new CancellationTokenSource ()) {
                    client.Connect (uri, cancel.Token);
                    client.Authenticate (credentials, cancel.Token);

                    // Open the Inbox folder
                    client.Inbox.Open (FolderAccess.ReadOnly, cancel.Token);

                    var query = SearchQuery.FromContains ("xxx@yahoo.com");
                    foreach (var uid in client.Inbox.Search (query, cancel.Token)) {
                        var message = client.Inbox.GetMessage (uid, cancel.Token);
                    }

                    client.Disconnect (true, cancel.Token);
                }
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top