Advertisement
Guest User

Untitled

a guest
Nov 1st, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. class ConnectPop3
  2. {
  3. private string username = "micorreo@gmail.com";
  4.  
  5. private string password = "miclave";
  6.  
  7. private int port = 995;
  8.  
  9. private string hostname = "pop.gmail.com";
  10.  
  11. private bool useSsl = true;
  12.  
  13. public List<Message> getMensajes()
  14. {
  15. try
  16. {
  17.  
  18. // El cliente se desconecta al terminar el using
  19. using (Pop3Client client = new Pop3Client())
  20. {
  21. // conectamos al servidor
  22. client.Connect(hostname, port, useSsl);
  23.  
  24. // Autentificación
  25. client.Authenticate(username, password, OpenPop.Pop3.AuthenticationMethod.UsernameAndPassword);
  26.  
  27. // Obtenemos los Uids mensajes
  28. List<string> uids = client.GetMessageUids();
  29.  
  30. // creamos instancia de mensajes
  31. List<Message> lstMessages = new List<Message>();
  32.  
  33. // Recorremos para comparar
  34. for (int i = 0; i < uids.Count; i++)
  35. {
  36. //obtenemos el uid actual, es él id del mensaje
  37. string currentUidOnServer = uids[i];
  38.  
  39. //por medio del uid obtenemos el mensaje con el siguiente metodo
  40. Message oMessage = client.GetMessage(i + 1);
  41.  
  42. //agregamos el mensaje a la lista que regresa el metodo
  43. lstMessages.Add(oMessage);
  44.  
  45. }
  46.  
  47. // regresamos la lista
  48. return lstMessages;
  49. }
  50. }
  51.  
  52. catch (Exception ex)
  53. {
  54. //si ocurre una excepción regresamos null, es importante que cachen las excepciones, yo
  55. //lo hice general por modo de ejemplo
  56. return null;
  57. }
  58. }
  59. }
  60.  
  61. protected void LeerCorreo_Click(object sender, EventArgs e)
  62. {
  63.  
  64. ConnectPop3 oCP3 = new ConnectPop3();
  65.  
  66. //invocamos el metodo para obtener mensajes
  67. List<OpenPop.Mime.Message> lstMensajes = oCP3.getMensajes();
  68.  
  69. //recorremos y mostramos el asunto
  70. foreach (OpenPop.Mime.Message oMensaje in lstMensajes)
  71. {
  72. //Console.WriteLine(oMensaje.Headers.Subject);
  73. MessageBox.Show(oMensaje.Headers.Subject);
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement