4

I have a need to find a particular email on a Google IMAP server and then save the attachments from the email. I think I have it all figured out except for the part of determining what the file name is of the attachment.

Below is my code so far, I am hoping that someone can point me in the right direction to determine the file name.

I have Googled and SO'd but have not been able to find something using the attachment approach.

internal class MailKitHelper
{
    private void SaveAttachementsForMessage(string aMessageId)
    {
        ImapClient imapClient = new ImapClient();
        imapClient.Connect("imap.google.com", 993, SecureSocketOptions.Auto);
        imapClient.Authenticate("xxxx", "xxxx");

        HeaderSearchQuery searchCondition = SearchQuery.HeaderContains("Message-Id", aMessageId);
        imapClient.Inbox.Open(FolderAccess.ReadOnly);
        IList<UniqueId> ids = imapClient.Inbox.Search(searchCondition);

        foreach (UniqueId uniqueId in ids)
        {
            MimeMessage message = imapClient.Inbox.GetMessage(uniqueId);

            foreach (MimeEntity attachment in message.Attachments)
            {
                attachment.WriteTo("WhatIsTheFileName"); //How do I determine the file name
            }
        }
    }
}
Community
  • 1
  • 1
TheEdge
  • 9,291
  • 15
  • 67
  • 135

1 Answers1

9

And the winner is.....

attachment.ContentDisposition.FileName
TheEdge
  • 9,291
  • 15
  • 67
  • 135
  • Was just writing post to say this answer or you could use `MimePart` instead of `MimeEntity` and the FileName property is exposed on that. – Marc Harry Sep 09 '15 at 11:54