JavaMail 刪除電子郵件
在本章中,我們將看到如何使用JavaMail API來刪除電子郵件。刪除信息涉及與該消息相關聯的標誌工作。有不同的標誌為不同的狀態,一些係統定義和一些用戶定義的。預定義的標誌在內部類中定義的標誌。標誌如下所列:
-
Flags.Flag.ANSWERED
-
Flags.Flag.DELETED
-
Flags.Flag.DRAFT
-
Flags.Flag.FLAGGED
-
Flags.Flag.RECENT
-
Flags.Flag.SEEN
-
Flags.Flag.USER
POP協議支持的消息的唯一刪除。
其次在刪除程序的基本步驟是:
-
獲取Session對象與POP和SMTP伺服器的細節的屬性。我們需要的POP細節來檢索信息和SMPT詳細信息發送郵件。
-
創建POP3存儲對象,並連接到存儲。
-
創建文件夾對象,並在READ_WRITE模式下郵箱打開相應的文件夾。
-
從收件箱文件夾中檢索郵件。
-
遍曆的消息,如果你想通過Message對象上調用方法setFlag(Flags.Flag.DELETED, true)以刪除郵件中鍵入“Y”或“y”。
-
這些消息標記DELETED 實際上並冇有刪除,直到我們調用Folder對象上expunge() 方法,或expunge 設置為true,關閉文件夾。
-
關閉存儲對象。
創建Java類
創建一個Java類文件ForwardEmail,是其內容如下:
package com.yiibai; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Properties; import javax.mail.Flags; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.NoSuchProviderException; import javax.mail.Session; import javax.mail.Store; public class DeleteEmail { public static void delete(String pop3Host, String storeType, String user, String password) { try { // get the session object Properties properties = new Properties(); properties.put("mail.store.protocol", "pop3"); properties.put("mail.pop3s.host", pop3Host); properties.put("mail.pop3s.port", "995"); properties.put("mail.pop3.starttls.enable", "true"); Session emailSession = Session.getDefaultInstance(properties); // emailSession.setDebug(true); // create the POP3 store object and connect with the pop server Store store = emailSession.getStore("pop3s"); store.connect(pop3Host, user, password); // create the folder object and open it Folder emailFolder = store.getFolder("INBOX"); emailFolder.open(Folder.READ_WRITE); BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); // retrieve the messages from the folder in an array and print it Message[] messages = emailFolder.getMessages(); System.out.println("messages.length---" + messages.length); for (int i = 0; i < messages.length; i++) { Message message = messages[i]; System.out.println("---------------------------------"); System.out.println("Email Number " + (i + 1)); System.out.println("Subject: " + message.getSubject()); System.out.println("From: " + message.getFrom()[0]); String subject = message.getSubject(); System.out.print("Do you want to delete this message [y/n] ? "); String ans = reader.readLine(); if ("Y".equals(ans) || "y".equals(ans)) { // set the DELETE flag to true message.setFlag(Flags.Flag.DELETED, true); System.out.println("Marked DELETE for message: " + subject); } else if ("n".equals(ans)) { break; } } // expunges the folder to remove messages which are marked deleted emailFolder.close(true); store.close(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException io) { io.printStackTrace(); } } public static void main(String[] args) { String host = "pop.gmail.com";// change accordingly String mailStoreType = "pop3"; String username = "abc@gmail.com";// change accordingly String password = "*****";// change accordingly delete(host, mailStoreType, username, password); } }
您可以通過取消注釋語句上設置調試 emailSession.setDebug(true);
編譯並運行
現在,我們的類是準備好了,讓我們編譯上麵的類。我已經保存了類DeleteEmail.java目錄: /home/manisha/JavaMailAPIExercise. 我們需要javax.mail.jar 和 activation.jar 在 classpath 中。執行下麵的命令從命令提示符編譯類(兩個jar 放置在 /home/manisha/ 目錄下):
javac -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: DeleteEmail.java
現在,這個類被編譯,執行下麵的命令來運行:
java -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: DeleteEmail
驗證輸出
你應該看到下麵的消息命令控製台上:
messages.length---1 --------------------------------- Email Number 1 Subject: Testing From: ABC <abc@gmail.com> Do you want to delete this message [y/n] ? y Marked DELETE for message: Testing