Exemple #1
0
        public static void TestPop3(string username, string password)
        {
            using (MailKit.MailSpool client = new MailKit.Net.Pop3.Pop3Client())
            {
                // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                // Non-Encrypted: 110
                // SSL/TLS:       995
                //client.Connect(RedmineMailService.Trash.UserData.POP, 110, false);
                client.Connect(RedmineMailService.Trash.UserData.POP, 995, true);

                // Note: since we don't have an OAuth2 token, disable
                // the XOAUTH2 authentication mechanism.
                client.AuthenticationMechanisms.Remove("XOAUTH2");

                client.Authenticate(username, password);

                for (int i = 0; i < client.Count; i++)
                {
                    MimeKit.MimeMessage message = client.GetMessage(i);
                    System.Console.WriteLine("Subject: {0}", message.Subject);
                } // Next i

                client.Disconnect(true);
            } // End Using client
        }     // End Sub TestPop3
        /**
         * Conectar no servidor para fazer o download dos e-mails
         */

        public async Task <List <LogEmail> > downloadAsync(string host, int nroPorta, bool flagSSL, string usuario, string senha, List <string> emailJaBaixados)
        {
            //Criar lista para armazenar as novas mensagens.
            List <LogEmail> listaNovasMensagens = new List <LogEmail>();

            Task <List <LogEmail> > OTask = new Task <List <LogEmail> >(() => {
                using (MailKit.Net.Pop3.Pop3Client client = new MailKit.Net.Pop3.Pop3Client()) {
                    client.Connect(host, nroPorta, flagSSL);
                    client.Authenticate(usuario, senha);
                    client.AuthenticationMechanisms.Remove("XOAUTH2");

                    int limit = client.Count;
                    int lastcount;

                    for (int i = 0; i < limit; i++)
                    {
                        lastcount        = (limit - 1) - i;
                        var NovaMensagem = client.GetMessage(lastcount);

                        LogEmail NovoEmail = this.mapper(NovaMensagem, lastcount);

                        bool flagSucesso = this.salvarEmail(NovoEmail);

                        if (flagSucesso)
                        {
                            client.DeleteMessage(lastcount);
                        }
                    }

                    client.Disconnect(true);
                }

                return(listaNovasMensagens);
            });

            OTask.Start();

            try {
                listaNovasMensagens = await OTask;
            } catch (Exception ex) {
                UtilLog.saveError(ex.GetBaseException(), String.Concat(ex.Message, "\n", ex.StackTrace));
            }

            return(listaNovasMensagens);
        }