import java.io.bufferedreader;
import java.io.inputstreamreader;
import java.util.calendar;
import java.util.date;
import java.util.properties;
import javax.mail.flags.flag;
import javax.mail.folder;
import javax.mail.message;
import javax.mail.session;
import javax.mail.store;
import javax.mail.urlname;
import javax.mail.internet.mimemessage;
import javax.mail.internet.mimeutility;
import javax.mail.search.andterm;
import javax.mail.search.bodyterm;
import javax.mail.search.comparisonterm;
import javax.mail.search.fromstringterm;
import javax.mail.search.integercomparisonterm;
import javax.mail.search.notterm;
import javax.mail.search.orterm;
import javax.mail.search.searchterm;
import javax.mail.search.sentdateterm;
import javax.mail.search.sizeterm;
import javax.mail.search.subjectterm;
/**
* 搜索邮件
*/ public class searchmailtest {
public static void main(string[] args)
throws exception {
properties props =
new properties();
props.setproperty("mail.pop3.auth", "true");
session session = session.getinstance(props);
urlname url =
new urlname("pop3", "pop3.163.com", 110,
null, "xyang81@163.com", "yx546900873");
store store = session.getstore(url);
store.connect();
// 得到收件箱
folder folder = store.getfolder("inbox");
// 以读写模式打开收件箱
folder.open(folder.read_write);
message[] messages = search(folder);
system.out.println("收件箱中共有:" folder.getmessagecount() "封邮件,搜索到" messages.length "封符合条件的邮件!");
// 解析邮件搜索到的邮件
pop3receivemailtest.parsemessage(messages);
// 根据用户输入的条件搜索所有邮件,并提示用户是否删除
//searchdemo(folder);
folder.close(
true);
store.close();
}
www.2cto.com
public static message[] search(folder folder)
throws exception {
// 搜索主题包含美食的邮件
string subject = "java培训";
searchterm subjectterm =
new subjectterm(subject);
// 搜索发件人包含支付宝的邮件
searchterm fromterm =
new fromstringterm("支付宝");
// 搜索邮件内容包含"招聘"的邮件
searchterm bodyterm =
new bodyterm("招聘");
// 搜索发件人不包含“智联招聘”的邮件
searchterm notterm =
new notterm(
new fromstringterm("智联招聘"));
// 搜索发件人为“智联招聘”,而且内容包含“java工程师“的邮件
searchterm andterm =
new andterm(
new fromstringterm("智联招聘"),
new bodyterm("java工程师"));
// 搜索发件人为”智联招聘“或主题包含”最新职位信息“的邮件
searchterm orterm =
new orterm(
new fromstringterm("智联招聘"),
new subjectterm("最新职位信息"));
// 搜索周一到今天收到的的所有邮件
calendar calendar = calendar.getinstance();
calendar.set(calendar.day_of_week, calendar.get(calendar.day_of_week - (calendar.day_of_week - 1)) - 1);
date mondaydate = calendar.gettime();
searchterm comparisontermge =
new sentdateterm(comparisonterm.ge, mondaydate);
searchterm comparisontermle =
new sentdateterm(comparisonterm.le,
new date());
searchterm comparisonandterm =
new andterm(comparisontermge, comparisontermle);
// 搜索大于或等100kb的所有邮件
int mailsize = 1024 * 100;
searchterm intcomparisonterm =
new sizeterm(integercomparisonterm.ge, mailsize);
return folder.search(intcomparisonterm);
}
}
实例:根据用户输入的收件人(email地址或姓名)和主题作为搜索条件,并提示用户是否删除搜索到的邮件?
[java] view plaincopy
/**
* 根据用户输入的收件人地址(包括email地址和姓名)和主题作为搜索条件,并提示用户是否删除搜索到的邮件
* @param from 收件人
* @param subject 主题
*/ public static void searchdemo(folder folder)
throws exception {
string notifymsg = "收件箱中一共有" folder.getmessagecount() "封邮件。请选择操作:\n";
notifymsg = "1、输入收件人\n" "2、输入主题\n" "3、开始搜索\n" "4、退出";
system.out.println(notifymsg);
string from =
null;
string subject =
null;
bufferedreader reader =
new bufferedreader(
new inputstreamreader(system.in));
string oper = reader.readline().trim();
while(!(from !=
null && subject !=
null)) {
if ("4".equals(oper)) {
system.exit(0);
}
else {
if ("1".equals(oper)) {
system.out.print("请输入收件人:");
from = reader.readline();
}
else if ("2".equals(oper)) {
system.out.print("请输入主题:");
subject = reader.readline();
}
else if ("3".equals(oper)) {
if (from ==
null || subject ==
null) {
system.out.println("未输入搜索条件,无法进行搜索!");
}
else {
break;
}
}
}
system.out.print("请选择操作:");
oper = reader.readline().trim();
}
system.out.println("\n系统正在根据搜索条件查询所有邮件,请稍候
\n");
// 根据输入的条件,创建searchterm实例对象
searchterm orterm =
new orterm(
new fromstringterm(from),
new subjectterm(subject)
);
// 根据搜索条件得到搜索到的邮件列表
message[] messages = folder.search(orterm);
system.out.println("共搜索到" messages.length "封满足搜索条件的邮件!\n\n请选择操作:1、查看邮件\t 2、删除所有邮件");
string deletequestion = "是否要删除搜索到的邮件?(yes/no)";
string searchresultoper = reader.readline();
if ("1".equals(searchresultoper)) {
for (message message : messages) {
mimemessage msg = (mimemessage) message;
string sub = pop3receivemailtest.getsubject(msg);
system.out.println("开始查看第" msg.getmessagenumber() "封邮件
");
system.out.println("主题: " sub);
system.out.println("发件人: " pop3receivemailtest.getfrom(msg));
system.out.println("收件人:" pop3receivemailtest.getreceiveaddress(msg,
null));
system.out.println("发送时间:" pop3receivemailtest.getsentdate(msg,
null));
system.out.println(deletequestion);
string answer = reader.readline();
if ("yes".equals(answer)) {
msg.setflag(flag.deleted,
true);
system.out.println("邮件[" sub "]删除成功!");
}
else if ("no".equals(answer)) {
system.out.println("第" msg.getmessagenumber() "封邮件查看完成!");
}
else if ("stop".equals(answer)) {
system.exit(0);
}
system.out.println();
}
}
else {
system.out.println(deletequestion);
string answer = reader.readline();
if ("yes".equals(answer)) {
for (message message : messages) {
string sub = mimeutility.decodetext(message.getsubject());
message.setflag(flag.deleted,
true);
system.out.println("邮件[" sub "]删除成功!");
}
}
}
}
}