What I want to do is read a type of attached file, for example those that are .pdf, from those emails that have not been read. To start, I was trying a simple example to read, but it tells me the following message:
Server did not accept user credentials
And I'm sure the username and password are correct.
The simple example I was testing with would be this (It's a class I made):
class ConnectPop3
{
private string username = "[email protected]";
private string password = "miclave";
private int port = 995;
private string hostname = "pop.gmail.com";
private bool useSsl = true;
public List<Message> getMensajes()
{
try
{
// El cliente se desconecta al terminar el using
using (Pop3Client client = new Pop3Client())
{
// conectamos al servidor
client.Connect(hostname, port, useSsl);
// Autentificación
client.Authenticate(username, password, OpenPop.Pop3.AuthenticationMethod.UsernameAndPassword);
// Obtenemos los Uids mensajes
List<string> uids = client.GetMessageUids();
// creamos instancia de mensajes
List<Message> lstMessages = new List<Message>();
// Recorremos para comparar
for (int i = 0; i < uids.Count; i++)
{
//obtenemos el uid actual, es él id del mensaje
string currentUidOnServer = uids[i];
//por medio del uid obtenemos el mensaje con el siguiente metodo
Message oMessage = client.GetMessage(i + 1);
//agregamos el mensaje a la lista que regresa el metodo
lstMessages.Add(oMessage);
}
// regresamos la lista
return lstMessages;
}
}
catch (Exception ex)
{
//si ocurre una excepción regresamos null, es importante que cachen las excepciones, yo
//lo hice general por modo de ejemplo
return null;
}
}
}
And from the index I call it like this:
protected void LeerCorreo_Click(object sender, EventArgs e)
{
ConnectPop3 oCP3 = new ConnectPop3();
//invocamos el metodo para obtener mensajes
List<OpenPop.Mime.Message> lstMensajes = oCP3.getMensajes();
//recorremos y mostramos el asunto
foreach (OpenPop.Mime.Message oMensaje in lstMensajes)
{
//Console.WriteLine(oMensaje.Headers.Subject);
MessageBox.Show(oMensaje.Headers.Subject);
}
}
I would like to know how to solve the Server did not accept user credentials message. Then I would like to be able to read the mail that has not been read in the inbox, but of those mails, only the attachments with a specific format.
Then I tried this way: Currently I'm testing it like this:
ImapClient ic = new ImapClient("imap.gmail.com", "[email protected]", "miclave",
AE.Net.Mail.AuthMethods.Login, 993, true);
// Select a mailbox. Case-insensitive
ic.SelectMailbox("INBOX");
Console.WriteLine(ic.GetMessageCount());
System.Windows.Forms.MessageBox.Show(ic.GetMessageCount().ToString());
MailMessage[] mm = ic.GetMessages(0, 10);
foreach (MailMessage m in mm)
{
Console.WriteLine(m.Subject);
System.Windows.Forms.MessageBox.Show(m.Subject);
}
// Probably wiser to use a using statement
ic.Dispose();
but I need to read the ones that have not been read from the inbox, but in this case it reads them all.
To read Gmail emails using Imap, I would use the AE.Net.Mail library (you have it available in NuGet) and the code would be something like this:
Note that for this to work, you must have port 993 open.
Edit The following code gets the id's of the unread emails and then gets the filenames of the attachments:
I hope this example helps you.