title: Problems with reading some attachments using IMAP protocol in C #
body: <p>I would like to ask you the following question:</p>
  
  <p>I have two codes to read emails with their respective attachments that have:</p>
  
  <p><strong>The first one, I use the S22.IMAP dll:</strong></p>
  
  <pre><code>using (ImapClient Client = new ImapClient(imap, 993, usuario, psw, AuthMethod.Login, true))
  {
      IEnumerable&lt;uint&gt; uids = Client.Search(SearchCondition.Unseen());//Correos no leídos
  
      IEnumerable&lt;MailMessage&gt; messages = Client.GetMessages(uids, FetchOptions.Normal); 
  
      conexion.stringconeccion = stringconeccion;
      conexion.conectar();
  
      String ts = "start transaction";
  
      MySqlCommand datos_ts = new MySqlCommand(ts, conexion.con);
      datos_ts.ExecuteScalar();
  
      DataTable dt_existeXML = new DataTable();
  
      int insercion = 0;
  
      foreach (MailMessage msg in messages)
      {
          foreach (Attachment atc in msg.Attachments)
          {
              if (System.IO.Path.GetExtension(msg.Attachments[0].Name) == ".xml")
              {
                  String archivoXML_texto = "";
                  byte[] allBytes = new byte[msg.Attachments[0].ContentStream.Length];
                  int bytesRead = msg.Attachments[0].ContentStream.Read(allBytes, 0, (int)msg.Attachments[0].ContentStream.Length);
  
                  using (MemoryStream memory = new MemoryStream(allBytes))
                  {
                      StreamReader archivoXML = new StreamReader(memory);
  
                      archivoXML_texto = archivoXML.ReadToEnd();
                      archivoXML.Close();
  
                      memory.Dispose();
                  }
              }
          }
      }
  </code></pre>
  
  <p>second code, using the MailKit DLL:</p>
  
  <pre><code>using (var client = new ImapClient ()) {
      client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);
      client.Authenticate ("correo@gmail.com", "clave");
      client.Inbox.Open (FolderAccess.ReadOnly);
  
      var uids = client.Inbox.Search(SearchQuery.NotSeen);
  
      foreach (var uid in uids)
      {
          var message = client.Inbox.GetMessage(uid);
          foreach (var attachment in message.Attachments.OfType&lt;MimePart&gt;())
          {   
                  byte[] allBytes = new byte[attachment.Content.Stream.Length];
                  int bytesRead = attachment.Content.Stream.Read(allBytes, 0, (int)attachment.Content.Stream.Length);
                  string texto_definitivo = "";
                  String archivoXML_textoBase64 = "";
                  using (MemoryStream memory = new MemoryStream(allBytes))
                  {
                      StreamReader archivoXML = new StreamReader(memory);
                      archivoXML_textoBase64 = archivoXML.ReadToEnd();
                      byte[] temp_backToBytes = Convert.FromBase64String(archivoXML_textoBase64);
                      texto_definitivo = Encoding.ASCII.GetString(temp_backToBytes);
                      archivoXML.Close();
                      memory.Dispose();
                  }   
          }
      }
      client.Disconnect (true);
  }
  </code></pre>
  
  <p>But I realize that there are attachments that do not read them, but also, I have noticed something in common of the attachments that are not read, do not have the attachment icon. But when I open the mail, I see that they have attachments:</p>
  
  <p><a href="https://i.stack.imgur.com/ajA0g.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>For example the image that is marked with red, the attachment icon to its right is not shown. But when I open it, I verify that it has an attached document. Just those documents, either of the two codes, do not read the attachments.</p>
  
  <p><strong>My question are:</strong></p>
  
  <p>How could I also read those attachments? 
  Should I configure or enable any option in the mail? What and how? 
  Is it a mistake to send mail? 
  How could it be solved?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I hope I have understood. The following code tried to obtain the text / content of the attachment for the cases that it could. I would like it if I'm wrong, please correct me.</p>
  
  <pre><code>    public static void DownloadBodyParts ()
      {
          using (var client = new ImapClient ()) {
              client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);             
              client.Authenticate("correo@gmail.com", "clave");
              client.Inbox.Open(FolderAccess.ReadWrite);
              var uids = client.Inbox.Search(SearchQuery.NotSeen);          
  
              foreach (var uid in uids)
              {
                  var message = client.Inbox.GetMessage(uid);                    
                  var attachments = message.BodyParts.OfType&lt;MimePart&gt;().Where(part =&gt; !string.IsNullOrEmpty(part.FileName));
  
                  foreach (MimePart atch in attachments)
                  {
                      using (var memory = new MemoryStream())
                      {
                          atch.Content.DecodeTo(memory);
                          var buffer = memory.ToArray();
                          var text = Encoding.UTF8.GetString(buffer);
                      }
                  }
               }
          client.Disconnect (true);
           }
  </code></pre>