javaMail 接收邮件,获取pop3协议收件箱后,遍历Message对象,无法获取主题,发送时间等信息,可获取邮件的内容,使用imap协议获取收件箱,可获得主题,发送时间,但无法获取邮件内容。测试了163,126,sohu邮箱,也测试自己搭建的SendMail邮件服务器,都存在这样的问题。
pop3协议代码与结果
Properties props = new Properties();
// 创建Session实例对象
Session session = Session.getDefaultInstance(props);
session.setDebug(false);
Store store = session.getStore("pop3");
store.connect("imap.163.com","username", "passwd");
// 获得收件箱
Folder folder = store.getFolder("INBOX"); // 收件箱
folder.open(Folder.READ_ONLY); //打开收件箱
System.out.println("未读邮件数: " + folder.getUnreadMessageCount());
System.out.println("删除邮件数: " + folder.getDeletedMessageCount());
System.out.println("新邮件: " + folder.getNewMessageCount());
// 获得收件箱中的邮件总数
System.out.println("邮件总数: " + folder.getMessageCount());
Message[] messages = folder.getMessages();
for (int i = 0, count = messages.length; i < count; i++) {
System.out.println("主题:"+messages.getSubject());
System.out.println("发件日期:"+messages.getSentDate());
System.out.println("邮件内容:"+messages.getContent());
}
//释放资源
folder.close(false);
store.close();
未读邮件数: 4
删除邮件数: 0
新邮件: 0
邮件总数: 4
主题:null
发件日期:null
邮件内容:------=_Part_0_330459891.1411535180732
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64
5rWL6K+V6YKu5Lu25paH5pys5L2T
------=_Part_0_330459891.1411535180732--
主题:null
发件日期:null
邮件内容:111111111123232
imap 协议代码与结果
Properties props = new Properties();
// 创建Session实例对象
Session session = Session.getDefaultInstance(props);
session.setDebug(false);
Store store = session.getStore("imap");
store.connect("imap.163.com","username", "passwd");
// 获得收件箱
Folder folder = store.getFolder("INBOX"); // 收件箱
folder.open(Folder.READ_ONLY); //打开收件箱
System.out.println("未读邮件数: " + folder.getUnreadMessageCount());
System.out.println("删除邮件数: " + folder.getDeletedMessageCount());
System.out.println("新邮件: " + folder.getNewMessageCount());
// 获得收件箱中的邮件总数
System.out.println("邮件总数: " + folder.getMessageCount());
Message[] messages = folder.getMessages();
for (int i = 0, count = messages.length; i < count; i++) {
System.out.println("主题:"+messages.getSubject());
System.out.println("发件日期:"+messages.getSentDate());
System.out.println("邮件内容:"+messages.getContent());
}
//释放资源
folder.close(false);
store.close();
未读邮件数: 1
删除邮件数: 0
新邮件: 1
邮件总数: 4
主题:测试邮件
发件日期:Wed Sep 24 13:06:20 CST 2014
获取内容抛异常
Exception in thread "main" java.lang.NullPointerException
at javax.mail.internet.ParameterList.set(ParameterList.java:165)
at com.sun.mail.imap.protocol.BODYSTRUCTURE.parseParameters(BODYSTRUCTURE.java:404)
at com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:224)
at com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:109)
at com.sun.mail.imap.protocol.FetchResponse.parseItem(FetchResponse.java:198)
at com.sun.mail.imap.protocol.FetchResponse.parse(FetchResponse.java:164)
at com.sun.mail.imap.protocol.FetchResponse.<init>(FetchResponse.java:92)
at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:288)
at com.sun.mail.iap.Protocol.command(Protocol.java:313)
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1543)
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1535)
at com.sun.mail.imap.protocol.IMAPProtocol.fetchBodyStructure(IMAPProtocol.java:1235)
at com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1294)
at com.sun.mail.imap.IMAPMessage.getDataHandler(IMAPMessage.java:639)
at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:927)
at cn.lonsun.filedirectpassing.demo.ReceiveMailTest.receive(ReceiveMailTest.java:66)
at cn.lonsun.filedirectpassing.demo.ReceiveMailTest.main(ReceiveMailTest.java:39)
|