/// <summary> /// - obtiene mensajes no leídos del servidor POP3 /// - how to download messages not seen before /// (notice that the POP3 protocol cannot see if a message has been read on the server /// before. Therefore the client need to maintain this state for itself) /// </summary> /// <param name="hostname">Hostname of the server. For example: pop3.live.com</param> /// <param name="port">Host port to connect to. Normally: 110 for plain POP3, 995 for SSL POP3</param> /// <param name="useSsl">Whether or not to use SSL to connect to server</param> /// <param name="username">Username of the user on the server</param> /// <param name="password">Password of the user on the server</param> /// <param name="seenUids">List of UID's of all messages seen before. New message UID's will be added to the list. Consider using a HashSet if you are using >= 3.5 .NET /// </param> /// <returns>A List of new Messages on the server</returns> public List <IMensajeEMail> FetchUnseenMessages(string hostname, int port, bool useSsl, string username, string password, List <string> seenUids) { try { iErr = 0; sMsj = string.Empty; // The client disconnects from the server when being disposed using (Pop3Client client = new Pop3Client()) { // Connect to the server client.Connect(hostname, port, useSsl); // Authenticate ourselves towards the server client.Authenticate(username, password); // Fetch all the current uids seen List <string> uids = client.GetMessageUids(); // Create a list we can return with all new messages //_lMensajes = new List<XDocument>(); _newXmlMessages = new List <IMensajeEMail>(); // Messages are numbered in the interval: [1, messageCount] // Ergo: message numbers are 1-based. // Most servers give the latest message the highest number int messageCount = uids.Count(); int primeros = 5; for (int i = messageCount; i > 0 && i > messageCount - primeros; i--) { string currentUidOnServer = uids[i - 1]; if (!seenUids.Contains(currentUidOnServer)) { // the uids list is in messageNumber order - meaning that the first // uid in the list has messageNumber of 1, and the second has // messageNumber 2. Therefore we can fetch the message using // i + 1 since messageNumber should be in range [1, messageCount] Message unseenMessage = client.GetMessage(i); IMensajeEMail mensajeRecibido = (IMensajeEMail) new MensajeEMail(unseenMessage); mensajeRecibido.FindXmlInMessage(); iErr = mensajeRecibido.IErr; // Add the message to the new messages if (iErr == 0) { _newXmlMessages.Add(mensajeRecibido); //_lMensajes.Add(_docXml); } // Add the uid to the seen uids, as it has now been seen seenUids.Add(currentUidOnServer); } } // Return our new found messages return(_newXmlMessages); } } catch (Exception fu) { iErr++; sMsj = "Excepción al leer correos. " + fu.Message + "[MonitorDeMensajes.FetchUnseenMessages]"; return(null); } }
/// <summary> /// Obtiene mensajes no leídos con adjunto xml del servidor POP3 en la lista _newXmlMessages /// Los mensajes no leídos que no tienen un xml adjunto están en la lista _seenUids /// (notice that the POP3 protocol cannot see if a message has been read on the server /// before. Therefore the client need to maintain this state for itself) /// 5/2/15 jcf Si encuentra un mail defectuoso lo salta y continúa con el resto /// </summary> /// <param name="hostname">Hostname of the server. For example: pop3.live.com</param> /// <param name="port">Host port to connect to. Normally: 110 for plain POP3, 995 for SSL POP3</param> /// <param name="useSsl">Whether or not to use SSL to connect to server</param> /// <param name="username">Username of the user on the server</param> /// <param name="password">Password of the user on the server</param> /// <param name="seenUids">List of UID's of all messages seen before. New message UID's will be added to the list. Consider using a HashSet if you are using >= 3.5 .NET /// </param> /// <returns>A List of new Messages on the server on _newMessages</returns> public void ObtieneMensajesXmlNoLeidos() { try { iErr = 0; sMsj = string.Empty; _newXmlMessages = new List <IMensajeEMail>(); // The client disconnects from the server when being disposed using (Pop3Client client = new Pop3Client()) { // Connect to the server client.Connect(_emailPop3, _emailPortIn, _Param.emailSsl); // Authenticate ourselves towards the server client.Authenticate(_Param.emailUser, _Param.emailPwd); var notSeenUids = uids.Except(_seenUids); //notSeenUids = notSeenUids.Where(x => int.Parse(x) > 19674); //test int numCorreos = notSeenUids.Count(); foreach (var unv in notSeenUids) { string currentUidOnServer = unv.ToString(); try { // the uids list is in messageNumber order - meaning that the first // uid in the list has messageNumber of 1, and the second has // messageNumber 2. Therefore we can fetch the message using // i + 1 since messageNumber should be in range [1, messageCount] int posiMensaje = uids.IndexOf(currentUidOnServer); Message unseenMessage = client.GetMessage(posiMensaje + 1); IMensajeEMail mensajeRecibido = (IMensajeEMail) new MensajeEMail(unseenMessage); mensajeRecibido.Uid = currentUidOnServer; bool existeXml = mensajeRecibido.FindXmlInMessage(); iErr = mensajeRecibido.IErr; // Add the message to the new messages if (iErr == 0 && existeXml) { _newXmlMessages.Add(mensajeRecibido); // Add the uid to the seen uids, as it has now been seen _seenUids.Add(currentUidOnServer); MuestraAvance(100 / numCorreos, "Correo recibido el " + mensajeRecibido.dateSent.ToString() + " Adjunto: " + mensajeRecibido.nombreArchivoXml); } else { //_newNotXmlMessages.Add(currentUidOnServer); MuestraAvance(100 / numCorreos, "Correo recibido sin adjunto el " + mensajeRecibido.dateSent.ToString()); } } catch (Exception em) { sMsj = "Excepción desconocida al abrir el correo con Id: " + currentUidOnServer + "[MonitorDeMensajes.ObtieneMensajesXmlNoLeidos] " + em.Message; //_seenUids.Add(currentUidOnServer); MuestraAvance(100 / numCorreos, sMsj); } } } } catch (Exception fu) { iErr++; sMsj = "Excepción al leer correos. " + fu.Message + "[MonitorDeMensajes.ObtieneMensajesXmlNoLeidos]"; } }