Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ConnectPop3
- {
- private string username = "micorreo@gmail.com";
- 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;
- }
- }
- }
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement