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 = "[email protected]";// 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 <[email protected]>
Do you want to delete this message [y/n] ? y
Marked DELETE for message: Testing