title: Using Mailkit to save attachments using IMAP
body: <p>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.</p>
  
  <p>Below is my code so far, I am hoping that someone can point me in the right direction to determine the file name.</p>
  
  <p>I have Googled and <a href="https://stackoverflow.com/questions/24455519/saving-attachments-using-mailkit-library">SO'd</a> but have not been able to find something using the attachment approach.</p>
  
  <pre><code>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&lt;UniqueId&gt; 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
              }
          }
      }
  }
  </code></pre>