【发布时间】:2014-12-16 05:43:23
【问题描述】:
这是我的代码:
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import java.util.List;
import java.util.ArrayList;
import java.util.regex.*;
public class EmailReader {
public static void main(String args[]) {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect ("imap.gmail.com", "email@gmail.com", "password");
System.out.println(store);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.getMessages();
for (Message message : messages) {
String subject = message.getSubject();
String location = "AAR|RRC|PSD|TPC|HP|SCC|MCA";
List<String> list = new ArrayList<String>();
System.out.println("SUBJECT: " + subject);
System.out.println("DATE: " + message.getSentDate());
Pattern pattern = Pattern.compile(location);
Matcher match = pattern.matcher(subject);
while (match.find()) {
list.add(match.group());
System.out.println(list);
}
}
} catch (MessagingException e) {
e.printStackTrace();
System.exit(2);
} catch (Exception e) {
e.printStackTrace();
System.exit(2);
}
}
}
我希望程序在搜索保存在位置中的字符串并将它们存储在数组中时读取每封电子邮件。这很好用,但是我希望将位置字符串中的每个缩写都设置为一个用于确定里程的值。我不知道我会怎么做。最后,我希望能够根据找到的数字总结某个日期范围内的电子邮件的所有值。因此,例如,如果 AAR 设置为 15 并出现在 3 个电子邮件主题行中,而 RRC 设置为 8 并出现在 2 个电子邮件主题行中,它将全部汇总并打印出来。
【问题讨论】: