JavaMail 删除电子邮件 - 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