Javamail Api 简明教程
JavaMail API - Deleting Emails
在本章中,我们将看到如何使用 JavaMail API 删除电子邮件。删除邮件涉及使用与邮件关联的标记。不同的状态具有不同的标记,一些由系统定义,一些由用户定义。预定义的标记在内部类 Flags.Flag 中定义,并列在下面:
-
Flags.Flag.ANSWERED
-
Flags.Flag.DELETED
-
Flags.Flag.DRAFT
-
Flags.Flag.FLAGGED
-
Flags.Flag.RECENT
-
Flags.Flag.SEEN
-
Flags.Flag.USER
POP 协议仅支持删除邮件。
删除程序中遵循的基本步骤:
-
在属性中获取具有 POP 和 SMPT 服务器详细信息的会话对象。我们需要 POP 详细信息来检索邮件,需要 SMPT 详细信息来发送邮件。
-
创建 POP3 存储对象并连接到存储。
-
创建 Folder 对象并在您的邮箱中以 READ_WRITE 模式打开相应的文件夹。
-
从收件箱文件夹中检索邮件。
-
遍历邮件并键入“Y”或“y”,如果您想通过对 Message 对象调用方法 setFlag(Flags.Flag.DELETED, true) 来删除邮件。
-
标记为 DELETED 的邮件不会实际删除,直到我们在 Folder 对象上调用 expunge() 方法或关闭 expunge 设置为 true 的文件夹为止。
-
Close the store object.
Create Java Class
创建 Java 类文件 ForwardEmail ,其内容如下:
package com.tutorialspoint;
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);
}
}
Compile and Run
现在我们的类已经准备好了,让我们编译上述类。我已经将类 DeleteEmail.java 保存到目录: /home/manisha/JavaMailAPIExercise 。我们需要类路径中的 jars javax.mail.jar 和 activation.jar。从命令提示符执行以下命令来编译类(两个 jars 都放在 /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