0 votes

Using this we can get required field only no need to fetch complete mail.

by (760 points)

1 Answer

+1 vote

You can use Imap.GetMessageInfoByUID(List uids) or its Sequence overload.

You'll get basic email info such as From, To, Subject and also obtain email structure e.g. attachment count, names:

https://www.limilabs.com/blog/get-email-information-from-imap-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)
In this method, all information is taken by the function and that is why it is not good for performance. I need flags with Uid only in one list for that I need to take all unwanted data.
Use Imap.GetFlagsByUID then.

GetMessageInfoByUID gets ENVELOPE, BODYSTRUCTURE and FLAGS. You can obtain each of those individually (GetFlagsByUID, GetBodyStructureByUID, GetEnvelopeByUID).
Great, Thank you.
...