+1 vote

Based on search I get a result as UID list. But I can only get one message at a time using the GetMessage calls.

Is there a way i can get multiple messages in a single call using a UID list?

by (250 points)

1 Answer

0 votes

You need to use foreach loop for that:

using(Imap imap = new Imap())
{
     imap.Connect("imap.example.com");   // or ConnectSSL for SSL
     imap.UseBestLogin("user", "password");

     imap.SelectInbox();
     List<long> uids = imap.Search(Flag.All);

     foreach (long uid in uids)
     {
           IMail email = new MailBuilder()
                .CreateFromEml(imap.GetMessageByUID(uid));

           string subject = email.Subject;
           int attachmentCount = email.Attachments.Count;


     }
     imap.Close();
}

If you only need most common email fields (e.g. you need to fill an list with all emails on the server) consider using GetMessageInfoByUID method. This method is very fast:

using (Imap imap = new Imap())
{
    imap.Connect("imap.example.com");   // or ConnectSSL
    imap.UseBestLogin("user", "password");

    imap.SelectInbox();
    List<long> uids = imap.Search(Flag.Unseen);
    List<MessageInfo> infos = imap.GetMessageInfoByUID(uids);

    foreach (MessageInfo info in infos)
    {
        string subject = info.Envelope.Subject;
        IList<MailBox> from = info.Envelope.From;
        IList<MailAddress> to = info.Envelope.To;

        foreach (MimeStructure attachment in info.BodyStructure.Attachments)
        {
            string fileName = attachment.SafeFileName;
            long size = attachment.Size;
        }
    }
    imap.Close();
}
by (301k points)
Thanks for your reply. I basically need a GetMessageByUID that can take a UID list. Trying to remove the need to cycle through each uid and asking for each message. So is there a way to send a list and get all messages steamed at once without looping.Need the full messages not just the info
No it is not possible. As messages are usually quite large it wouldn't give you significant performance improvement.
...