When I read an email, he does not read me some attachments. I use the IMAP protocol which contains the S22.IMAP dll

1

I have the following code, in which I use the IMAP protocol that contains the S22.IMAP dll:

using (ImapClient client = new ImapClient(imap, 993,
               usuario, psw, AuthMethod.Login, true))
        {
            foreach (var uid in client.Search(SearchCondition.Unseen()))
            {
                var message = client.GetMessage(uid);                      
                foreach (Attachment atc in message.Attachments)
                { 
                  if (System.IO.Path.GetExtension(atc.Name) == ".xml")
                    {
                        String archivoXML_texto = "";
                        byte[] allBytes = new byte[atc.ContentStream.Length];
                        int bytesRead = atc.ContentStream.Read(allBytes, 0, (int)atc.ContentStream.Length);
                        using (MemoryStream memory = new MemoryStream(allBytes))
                        {
                            StreamReader archivoXML = new StreamReader(memory);
                            archivoXML_texto = archivoXML.ReadToEnd();
                            archivoXML.Close();
                            memory.Dispose();
                        }
                  .......
                  .......
                   }
                }
             }
           }

but I have a problem, when I try to read the attachment of some mail, I realize in the debugging of the code that is not reading the attachment. The strange thing about this event is that it happens with some emails, as well as gmail and private emails (I do not say that all of these domains do not read the attachments), the others read them well.

I would like to know what would be the cause that prevents me from reading these emails. Could it be that there is some privacy settings, for example in gmail,  to have it block the attachments in the code ?, and if it were true, how would you enable it? Or should I change the way the code is implemented? And what would be the best or some good example?

Of course, thank you, I hope your help please.

    
asked by Danilo 27.03.2018 в 19:55
source

3 answers

1

Could be some bag in the library, you did not try using any other ?, For example

MailKit

This has an example where you download the attach

ImapExamples.cs

Analyze if the same effect happens with other libraries

    
answered by 27.03.2018 в 21:56
0

Check this answer in SO English.

You may be looking for unread messages in another way and then, retrieving the complete messages, the logic of your code will work.

For the purpose of making this answer more than just a link, I add the relevant code:

First this

List<string> ids = new List<string>();
List<AE.Net.Mail.MailMessage> mails = new List<AE.Net.Mail.MailMessage>();

using (var imap = new AE.Net.Mail.ImapClient("imap.gmail.com", mailAccount.UserName, mailAccount.Password, AE.Net.Mail.ImapClient.AuthMethods.Login, 993, true)) 
{
    var msgs = imap.SearchMessages(SearchCondition.Unseen());
    for (int i = 0; i < msgs.Length; i++) {
        string msgId = msgs[i].Uid;
        ids.Add(msgId);            
    }

    foreach (string id in ids)
    {
        mails.Add(imap.GetMessage(id, headersonly: false));
    }
}

And then this

foreach(var msg in mails)
{
    foreach (var att in msg.Attachments) 
    {
        string fName;
        fName = att.Filename;
    }
}
    
answered by 27.03.2018 в 23:06
0

I found a half solution:

using (var client = new ImapClient ()) {
            client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);
            client.Authenticate ("[email protected]", "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<MimePart>())
                {
                        String archivoXML_texto = "";
                        byte[] allBytes = new byte[attachment.Content.Stream.Length];
                        int bytesRead = attachment.Content.Stream.Read(allBytes, 0, (int)attachment.Content.Stream.Length);

                      // string texto_prueba=attachment.ContentBase

                        using (MemoryStream memory = new MemoryStream(allBytes))
                        {
                            StreamReader archivoXML = new StreamReader(memory);
                            archivoXML_texto = archivoXML.ReadToEnd();
                            archivoXML.Close();
                            memory.Dispose();
                        } 
                }
            }

But it has a great detail, when I want to get the text of the attachment, which contains the XMLX_text variable, it shows it to me in binary:

And what I want is to get the text or string of the attachment, as it appears in the photo:

As you can see, in the debugging, in the Text attribute, the content that I need to save in a variable type string, but the text of the attachment is transformed into binary text. Any recommendations?

Observation, I realized that it comes as base64.

-------------------------------------------- ----------------------------------------

Improved response update:

I was able to solve my problem, I leave the code, but it can be improved:

 public static void DownloadBodyParts ()
    {
        using (var client = new ImapClient ()) {
            client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);
            client.Authenticate ("[email protected]", "clave");
            client.Inbox.Open (FolderAccess.ReadOnly);

            //var query = SearchQuery.SubjectContains ("MimeKit").Or (SearchQuery.SubjectContains ("MailKit"));
            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<MimePart>())
                {                       
                    //if (System.IO.Path.GetExtension(attachment.FileName) == ".xml")
                    //{
                        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);
        }
    }

The objective is to be able to obtain the content / text of the attachment that comes in the mail, I realized that I was getting the binary in base64, so I had to convert the XMLX_textBase64 variable, which comes as a base64 string to binary and This is passed to string.

I imagine there will be some more direct way to do it, but that is the solution that came to my mind.

    
answered by 28.03.2018 в 22:28