title: How to download the attachments of a email using Mailkit
body: <p>I'm using the code from <a href="https://stackoverflow.com/questions/24455519/saving-attachments-using-mailkit-library">here</a> to download attachments from mail using MailKit. In the foreach loop where attachments are retrieved, it is always returning empty. As it is empty it is not entering the foreach loop. Please correct me if i'm doing anything wrong.</p>
  
  <pre><code>        var messages = client.Inbox.Fetch(0, -1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId);
          int unnamed = 0;
  
          foreach (var message in messages)
          {
              var multipart = message.Body as BodyPartMultipart;
              var basic = message.Body as BodyPartBasic;
  
              if (multipart != null)
              {
  
                  //Here the attachments is always empty even though my mail has email with attachements
                  var attachments = multipart.BodyParts.OfType&lt;BodyPartBasic&gt;().Where(x =&gt; x.IsAttachment);
                  foreach (var attachment in attachments)
                  {
                      var mime = (MimePart)client.Inbox.GetBodyPart(message.UniqueId.Value, attachment);
                      var fileName = mime.FileName;
  
                      if (string.IsNullOrEmpty(fileName))
                          fileName = string.Format("unnamed-{0}", ++unnamed);
  
                      using (var stream = File.Create(fileName))
                          mime.ContentObject.DecodeTo(stream);
                  }
              }
              else if (basic != null &amp;&amp; basic.IsAttachment)
              {
                  var mime = (MimePart)client.Inbox.GetBodyPart(message.UniqueId.Value, basic);
                  var fileName = mime.FileName;
  
                  if (string.IsNullOrEmpty(fileName))
                      fileName = string.Format("unnamed-{0}", ++unnamed);
  
                  using (var stream = File.Create(fileName))
                      mime.ContentObject.DecodeTo(stream);
              }
          }
  </code></pre>