Nuestro grupo de especialistas pasados algunos días de trabajo y recopilar de información, obtuvimos la respuesta, esperamos que te resulte útil para tu trabajo.
Solución:
Usando la biblioteca de: https://github.com/pmengal/MailSystem.NET
Aquí está mi ejemplo de código completo:
Repositorio de correo electrónico
using System.Collections.Generic;
using System.Linq;
using ActiveUp.Net.Mail;
namespace GmailReadImapEmail
public class MailRepository
private Imap4Client client;
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
Client.Login(login, password);
public IEnumerable GetAllMails(string mailBox)
return GetMails(mailBox, "ALL").Cast();
public IEnumerable GetUnreadMails(string mailBox)
return GetMails(mailBox, "UNSEEN").Cast();
protected Imap4Client Client
get return client ?? (client = new Imap4Client());
private MessageCollection GetMails(string mailBox, string searchPhrase)
Mailbox mails = Client.SelectMailbox(mailBox);
MessageCollection messages = mails.SearchParse(searchPhrase);
return messages;
Uso
[TestMethod]
public void ReadImap()
var mailRepository = new MailRepository(
"imap.gmail.com",
993,
true,
"[email protected]",
"yourPassword"
);
var emailList = mailRepository.GetAllMails("inbox");
foreach (Message email in emailList)
Console.WriteLine("0: 1
2
", email.From, email.Subject, email.BodyHtml.Text);
if (email.Attachments.Count > 0)
foreach (MimePart attachment in email.Attachments)
Console.WriteLine("Attachment: 0 1
", attachment.ContentName, attachment.ContentType.MimeType);
Otro ejemplo, esta vez usando MailKit
public class MailRepository : IMailRepository
private readonly string mailServer, login, password;
private readonly int port;
private readonly bool ssl;
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
this.mailServer = mailServer;
this.port = port;
this.ssl = ssl;
this.login = login;
this.password = password;
public IEnumerable GetUnreadMails()
var messages = new List();
using (var client = new ImapClient())
client.Connect(mailServer, port, ssl);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(login, password);
// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
var results = inbox.Search(SearchOptions.All, SearchQuery.Not(SearchQuery.Seen));
foreach (var uniqueId in results.UniqueIds)
var message = inbox.GetMessage(uniqueId);
messages.Add(message.HtmlBody);
//Mark message as read
//inbox.AddFlags(uniqueId, MessageFlags.Seen, true);
client.Disconnect(true);
return messages;
public IEnumerable GetAllMails()
var messages = new List();
using (var client = new ImapClient())
client.Connect(mailServer, port, ssl);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(login, password);
// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
var results = inbox.Search(SearchOptions.All, SearchQuery.NotSeen);
foreach (var uniqueId in results.UniqueIds)
var message = inbox.GetMessage(uniqueId);
messages.Add(message.HtmlBody);
//Mark message as read
//inbox.AddFlags(uniqueId, MessageFlags.Seen, true);
client.Disconnect(true);
return messages;
Uso
[Test]
public void GetAllEmails()
var mailRepository = new MailRepository("imap.gmail.com", 993, true, "[email protected]", "YOURPASSWORDHERE");
var allEmails = mailRepository.GetAllMails();
foreach(var email in allEmails)
Console.WriteLine(email);
Assert.IsTrue(allEmails.ToList().Any());
Tú no necesita cualquier extra Bibliotecas de terceros. Puede leer los datos de la API que Gmail ha proporcionado aquí: https://mail.google.com/mail/feed/atom
la respuesta en XML El formato puede ser manejado por el siguiente código:
try
System.Net.WebClient objClient = new System.Net.WebClient();
string response;
string title;
string summary;
//Creating a new xml document
XmlDocument doc = new XmlDocument();
//Logging in Gmail server to get data
objClient.Credentials = new System.Net.NetworkCredential("Email", "Password");
//reading data and converting to string
response = Encoding.UTF8.GetString(
objClient.DownloadData(@"https://mail.google.com/mail/feed/atom"));
response = response.Replace(
@"", @"");
//loading into an XML so we can get information easily
doc.LoadXml(response);
//nr of emails
var nr = doc.SelectSingleNode(@"/feed/fullcount").InnerText;
//Reading the title and the summary for every email
foreach (XmlNode node in doc.SelectNodes(@"/feed/entry"))
title = node.SelectSingleNode("title").InnerText;
summary = node.SelectSingleNode("summary").InnerText;
catch (Exception exe)
MessageBox.Show("Check your network connection");
¿Ha probado el cliente de correo electrónico POP3 con soporte MIME completo?
Si no lo hace, es un muy buen ejemplo para usted. Como alternativa;
OpenPop.NET
Biblioteca de clases .NET en C# para comunicarse con servidores POP3. Fácil de usar pero potente. Incluye un robusto analizador MIME respaldado por varios cientos de casos de prueba. Para obtener más información, visite la página de inicio de nuestro proyecto.
Lumisoft