This project is read-only.

Download attachments files from gmail

Topics: Issues
Jul 25, 2014 at 10:02 AM
Edited Jul 25, 2014 at 12:54 PM
Hello!
I am developing a Windows Phone 8.1 App and I am not able to download attachments files from a gmail account.
My code:
 var client = new ImapClient("imap.gmail.com", 993, true);

            if (client.Connect())
            {

                if (client.Login("mail@gmail.com", "pass"))
                {
                    //                   

                    // login successful
                    Folder folder = client.Folders.Inbox;

                    //client.Behavior.MessageFetchMode = MessageFetchMode.Basic;

                    List<Message> listaMensajes = new List<Message>();

                    Message[] mensajes = folder.Search("UNSEEN", ImapX.Enums.MessageFetchMode.Tiny);


                    foreach (Message mensaje in mensajes)
                    {


                        if (mensaje.From.Address.Equals("mymail@gmail.com") && mensaje.Attachments[0].ContentType.MediaType.Equals("application/pdf"))
                        {
                            listaMensajes.Add(mensaje);
                            //mensaje.Seen = true;
                            mensaje.Download(ImapX.Enums.MessageFetchMode.Attachments);
                            string nombreFichero = mensaje.Attachments[0].FileName;
                            mensaje.Attachments[0].Download();
                            byte[] fichero = mensaje.Attachments[0].FileData;
                         }
                  }
          }
   }
At the time mensaje.Attachments[0].Download(); is executing, i get this exception:

_A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.ni.dll

Additional information: Exception has been thrown by the target of an invocation._

If I implement this code in a console app, it runs correctly...

Thanks!
Jul 25, 2014 at 3:46 PM
Hi luperxe,

once you download the message using MessageFetchMode.Attachments, there is no need to download each attachment separately again. Try to change your code as following:
if (mensaje.From.Address.Equals("mymail@gmail.com") && mensaje.Attachments[0].ContentType.MediaType.Equals("application/pdf"))
{
    listaMensajes.Add(mensaje);
    //mensaje.Seen = true;
    mensaje.Attachments[0].Download();
    string nombreFichero = mensaje.Attachments[0].FileName; 
    byte[] fichero = mensaje.Attachments[0].FileData;
}
Greets,

Pavel