📅  最后修改于: 2020-11-14 07:25:22             🧑  作者: Mango
在本章中,我们将看到如何使用JavaMail API删除电子邮件。删除消息涉及使用与消息关联的标志。对于不同的状态,有不同的标志,其中一些是系统定义的,某些是用户定义的。预定义标志在内部类Flags.Flag中定义,并在下面列出:
Flags.Flag.ANSWERED
标志标记已删除
Flags.Flag.DRAFT
标志,旗帜
Flags.Flag.RECENT
Flags.Flag.SEEN
Flags.Flag.USER
POP协议仅支持删除邮件。
删除程序中遵循的基本步骤是:
在属性中获取带有POP和SMPT服务器详细信息的Session对象。我们将需要POP详细信息来检索消息,并需要SMPT详细信息来发送消息。
创建POP3存储对象并连接到该存储。
创建文件夹对象,并以READ_WRITE模式打开邮箱中的相应文件夹。
从收件箱文件夹中检索邮件。
如果要通过在Message对象上调用setFlag(Flags.Flag.DELETED,true)方法来删除消息,请遍历消息并键入“ Y”或“ y”。
标记为DELETED的消息实际上不会删除,直到我们在Folder对象上调用expunge()方法或关闭expunge设置为true的文件夹为止。
关闭商店对象。
创建一个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);
}
}
您可以通过取消注释emailSession.setDebug(true);注释来设置调试。
现在我们的课程已经准备好,让我们编译上面的课程。我将类DeleteEmail.java保存到目录: / home / manisha / JavaMailAPIExercise 。我们将在类路径中需要jars javax.mail.jar和activation.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
Do you want to delete this message [y/n] ? y
Marked DELETE for message: Testing