+1 vote

Is possibile to get the message detail (envelope and body) with the attachments metadata (id, name, size) but not the binary data?

We need to download the attachments separately.

by

1 Answer

0 votes
 
Best answer

Yes, it is possible. Use Imap.GetMessageInfoByUID method it retrieves ENVELOPE and BODYSTRUCTURE of specified messages.

Envelope contains most common information about the email message, such as: Subject, Date, From and To. BodySturcture contains email structure: number of attachments, attachment names, sizes and content-types. BodySturcture also includes information about Text and Html parts of the emails.

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

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

    foreach (MessageInfo info in infos)
    {
        Console.WriteLine("Subject: " + info.Envelope.Subject);
        Console.WriteLine("From: " + info.Envelope.From);
        Console.WriteLine("To: " + info.Envelope.To);
        foreach (MimeStructure att in info.BodyStructure.Attachments)
        {
            Console.WriteLine("  Attachment: '{0}' ({1} bytes)",
                att.SafeFileName,
                att.Size);
        }
        Console.WriteLine();
    }
    imap.Close();
}

Please read this article if you wish to download BodyStructure.Attachments or BodyStructure.Text/Html data.

You can find more details in Get email information from IMAP (fast) article.

by (301k points)
...