public class MailReceiver {
public static void main(String[] args) throws MessagingException, IOException {
String pop3Host = "pop3.sohu.com";
String protocol = "pop3";
String userName = "xxssyyyyssxx";
String passWord = "xsy881026";
Properties props = new Properties();
props.put("mail.store.protocol", protocol);
props.put("mail.pop3.host", pop3Host);
Session session = Session.getDefaultInstance(props,null);
session.setDebug(true);
Store store = session.getStore(protocol);
store.connect(pop3Host, userName,passWord);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Message[] messages = folder.search(new OrTerm(new FromStringTerm("
[email protected]"),
new SubjectTerm("test from javamail")));
//Message[] messages = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
String subject = message.getSubject();
String from = message.getFrom()[0].toString();
System.out.println("第 "+ i + " 封信的主题是:" + subject + ",发信人是: " + from);
System.out.println("你想读取吗?yes/no");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String yes = br.readLine();
if("yes".equalsIgnoreCase(yes)){
message.writeTo(System.out);
}
}
folder.close(false);
store.close();
}
}
让IE浏览器解析邮件正文部分
l
如果需要浏览器显示出邮件的
header
和
content
,一种方式是自己解析后生成
html
,一种方式是在页面中使用
frameset
,
每个
frame
访问不同的
servlet
,
servlet
完成不同的任务,比如一个解析
header
,一个直接像上面一样输出
content。