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
public List <EmailMessage> ReceiveEmail(int maxCount = 10) { using (var emailClient = new MailKit.Net.Pop3.Pop3Client()) { emailClient.Connect(_emailConfiguration.PopServer, _emailConfiguration.PopPort, true); emailClient.AuthenticationMechanisms.Remove("XOAUTH2"); emailClient.Authenticate(_emailConfiguration.PopUsername, _emailConfiguration.PopPassword); List <EmailMessage> emails = new List <EmailMessage>(); for (int i = 0; i < emailClient.Count && i < maxCount; i++) { var message = emailClient.GetMessage(i); var emailMessage = new EmailMessage { Content = !string.IsNullOrEmpty(message.HtmlBody) ? message.HtmlBody : message.TextBody, Subject = message.Subject }; emailMessage.ToAddresses.AddRange(message.To.Select(x => (MailboxAddress)x).Select(x => new EmailAddress { Address = x.Address, Name = x.Name })); emailMessage.FromAddresses.AddRange(message.From.Select(x => (MailboxAddress)x).Select(x => new EmailAddress { Address = x.Address, Name = x.Name })); } return(emails); } }
/** * 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); }