How to get email attachment meta data but not the content from Gmail?

1.9k Views Asked by At

I only need header and metadata of attachments from Gmail, I don't want to get email body or the content of the attachments, because they may be very big and I don't need them.

Is there a way to do so through AE.Net.Mail or gmail api? It seems only come with one option of of headers only, which will not fetch the attachment metadata for me.

MailMessage[] GetMessages(string startUID, string endUID, bool headersonly = true, bool setseen = false);
2

There are 2 best solutions below

0
On BEST ANSWER

Eventually, I gave up on AE.Net.Mail and switched to Gmail API instead. And I am able to get attachment meta data from the Message Get Request, without getting the actual attachment file.

https://developers.google.com/gmail/api/v1/reference/users/messages/get

0
On

If you use MailKit instead of AE.Net.Mail, you can get the metadata of attachments using the following code snippet:

using (var client = new ImapClient ()) {
    client.Connect ("imap.gmail.com", 993, true);
    client.Authenticate ("username", "password");

    client.Inbox.Open (FolderAccess.ReadWrite);

    var summaries = client.Fetch (0, -1, MessageSummaryItems.UniqueId |
        MessageSummaryItems.BodyStructure | // metadata for mime parts
        MessageSummaryItems.Envelope);      // metadata for messages

    foreach (var summary in summaries) {
        // Each summary item will have the UniqueId, Body and Envelope properties
        // filled in (since that's what we asked for).
        var uid = summary.UniqueId.Value;

        Console.WriteLine ("The UID of the message is: {0}", uid);
        Console.WriteLine ("The Message-Id is: {0}", summary.Envelope.MessageId);
        Console.WriteLine ("The Subject is: {0}", summary.Envelope.Subject);
        // Note: there are many more properties, but you get the idea...

        // Since you want to know the metadata for each attachment, you can
        // now walk the MIME structure via the Body property and get
        // whatever details you want to get about each MIME part.
        var multipart = summary.Body as BodyPartMultipart;
        if (multipart != null) {
            foreach (var part in multipart.BodyParts) {
                var basic = part as BodyPartBasic;

                if (basic != null && basic.IsAttachment) {
                    // See http://www.mimekit.net/docs/html/Properties_T_MailKit_BodyPartBasic.htm
                    // for more details on what properties are available.
                    Console.WriteLine ("The size of this attachment is: {0} bytes", basic.Octets);
                    Console.WriteLine ("The file name is: {0}", basic.FileName);

                    // If you want to download just this attachment, you can download it like this:
                    var attachment = client.Inbox.GetBodyPart (uid, basic);
                }
            }
        }
    }

    client.Disconnect (true);
}

Keep in mind that since MIME is a tree structure and not a flat list, you'll realistically want to walk multiparts recursively.