import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
public class DeleteMessageExample {
public static void main(String args[]) throws Exception {
//String host = "smtp.sina.com.cn";
String host = " mailserver";
String from = " username@mailserver.com ";
String to = "username@mailserver.com";
String username = "username";
String password = "***";
// 获取系统属性
// Properties props = System.getProperties();
// 很多例子中是这样的,其实下面这句更好,可以用在applet中
Properties props = new Properties();
// 设置 mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true"); //这样才能通过验证
// Get session
Session session = Session.getDefaultInstance(props);
//Session session = Session.getInstance(System.getProperties(), null);
// Get the store
Store store = session.getStore("pop3");
store.connect(host, username, password);
// Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
// Get directory
Message message[] = folder.getMessages();
for (int i = 0, n = message.length; i < n; i++) {
System.out.println(i + ": " + message[i].getFrom()[0] + "/t"
+ message[i].getSubject()+"/n"+
message[i].getContent());
System.out
.println("Do you want to delete message? [YES to delete]");
//注意邮件将被直接删除,而不是保留在服务器的垃圾箱中
String line = reader.readLine();
// Mark as deleted if appropriate
if ("YES".equals(line)) {
message[i].setFlag(Flags.Flag.DELETED, true);
}
}
// Close connection
folder.close(true);
store.close();
}
}