title: IMAP on C# - Download Mails and Attachments
body: <p>I have tried this on C# using open source project called "Koolwired.Imap" on sourceforge.</p>
  
  <p>It was OK when downloading mails, but for attachments it is only listing the file name of the attachment. Is there anyone who has tried this?</p>
  
  <p>If not is there any other better free library which can do the same (I need a free/open source solution for this, because I am doing this for my campus project)</p>
  
  <pre><code>ImapConnect connection = new ImapConnect("imap.gmail.com", 993, true);
  ImapCommand command = new ImapCommand(connection);
  ImapAuthenticate auth = new ImapAuthenticate(connection, "&lt;username&gt;@gmail.com", "&lt;password&gt;");
  connection.Open();
  auth.Login();
  
  string htmlbody = "";
  ImapMailbox mailbox = command.Select("INBOX");
  mailbox = command.Fetch(mailbox);
  int mailCount = mailbox.Messages.Count;
  
  for (int i = 0; i &lt; mailCount ; i++)
  {
   ImapMailboxMessage msg = mailbox.Messages[mailCount - 1];
   msg = command.FetchBodyStructure(msg);
  
   msg.BodyParts[0].ContentEncoding = BodyPartEncoding.NONE;
   msg = command.FetchBodyPart(msg, msg.HTML);
  
   foreach (ImapMessageBodyPart a in msg.BodyParts)
   {
       if (a.Data != null)
       {
           string fileName = "";
  
           if (a.Attachment) fileName = ParseFileName(a.Disposition);
               string mimeType = a.ContentType.MediaType.ToLower();
  
           a.ContentEncoding = BodyPartEncoding.UTF7;
           htmlbody = a.Data;
      }
   }
  }
  
  auth.Logout();
  connection.Close();
  </code></pre>