Rilevamento di nuovi messaggi email sul server POP3
Con gli account POP3 è possibile lasciare i messaggi sul server durante il download e la lettura. Lasciare le email sul server significa che sono disponibili per altre applicazioni e individui, ad esempio utenti che accedono alla loro posta da più dispositivi. Oppure potresti voler scaricare solo i messaggi che soddisfano criteri specifici, ad esempio messaggi con un determinato oggetto. Per gestire la posta, puoi:
- Leggi tutti i messaggi dal server di posta POP3 usando Aspose.Email.
- Scarica i messaggi nel tuo database locale.
I messaggi non vengono eliminati ma rimangono sul server. La prima volta che viene eseguito, il processo funziona correttamente. Tutti i messaggi necessari vengono scaricati nel database. Ma la seconda volta che viene eseguito, gli stessi messaggi vengono scaricati perché sono ancora sul server di posta. Ciò causa record duplicati. Per risolvere questo problema, usa il Pop3MessageInfo.UniqueID proprietà per verificare se un messaggio è già stato scaricato. L’ID univoco del messaggio deve essere memorizzato nel database: è la chiave di ricerca per il rilevamento di nuovi messaggi.
Rilevamento di nuovi messaggi
Per identificare le nuove email da un elenco di email su un server POP3:
- Connetti al server.
- Ottieni un elenco di email.
- Connetti al database.
- Ottieni un elenco di email.
- Confronta gli elenchi.
- Salva le nuove email nel database.
Il processo è più veloce quando:
- Recupera tutti gli ID univoci dei messaggi in una tabella hash.
- Cerca nella tabella hash invece che nel database per ogni messaggio email in un ciclo for(…).
Invece di interrogare il database per ogni messaggio, richiedendo molte chiamate al database, questo metodo richiede una sola chiamata. Il seguente frammento di codice mostra come rilevare i nuovi messaggi email sul server POP3.
public static void run() {
try {
// Connect to the POP3 mail server and check messages.
Pop3Client pop3Client = new Pop3Client("pop.domain.com", 993, "username", "password");
// List all the messages
Pop3MessageInfoCollection msgList = pop3Client.listMessages();
for (Pop3MessageInfo msgInfo : msgList) {
// Get the POP3 message's unique ID
String strUniqueID = msgInfo.getUniqueId();
// Search your local database or data store on the unique ID. If a match is found, that means it's already downloaded. Otherwise download and save it.
if (searchPop3MsgInLocalDB(strUniqueID) == true) {
// The message is already in the database. Nothing to do with this message. Go to next message.
} else {
// Save the message
savePop3MsgInLocalDB(msgInfo);
}
}
} catch (Exception ex) {
System.err.println(ex);
}
}
private static void savePop3MsgInLocalDB(Pop3MessageInfo msgInfo) {
// Open the database connection according to your database. Use public properties (for example msgInfo.Subject) and store in database,
// for example, " INSERT INTO POP3Mails (UniqueID, Subject) VALUES ('" + msgInfo.UniqueID + "' , '" + msgInfo.Subject + "') and Run the query to store in database.
}
private static boolean searchPop3MsgInLocalDB(String strUniqueID) {
// Open the database connection according to your database. Use strUniqueID in the search query to find existing records,
// for example, " SELECT COUNT(*) FROM POP3Mails WHERE UniqueID = '" + strUniqueID + "' Run the query, return true if count == 1. Return false if count == 0.
return false;
}