Java邮件不返回主题 [英] Java mail don't return subject
问题描述
我正在使用javamail营救收件箱中的邮件.我从收件箱中收到所有邮件,但是subject(message.getSubject())返回null.
I'm using javamail to rescue mails of inbox. I get all mails from inbox, but the subject(message.getSubject()) return null.
我在几个地点进行了研究,所有提出的解决方案都行不通.
I research in several sites and all proposed solutions not work.
仅在Tomcat6中会发生此问题,如果我在Java项目中对主要方法执行了偶数代码,则我得到了所有属性,包括主题.我是SSL协议用户(pop3s).
This problem occurs only in the Tomcat6, if a execute even code in java project for main method i get all atributis, include subject. I'm user de SSL protocol (pop3s).
感谢帮助
public void getMails(final String host, final int port, final String user, final String password){
final Session session = Session.getInstance(System.getProperties(), null);
final Store store = session.getStore("pop3s");
store.connect(host, port, user, password);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
int count = inbox.getMessageCount();
System.out.println(" Count Emails "+count);
final Message[] messages = inbox.getMessages();
for (final Message message : messages) {
//This return null
System.out.print("mail subject: " + message.getSubject() + " send at: " + message.getSentDate());
//This return body of mail
System.out.print("mail subject: " + message.getContent().toString());
}
直接在eclipse上我的Main方法的输出:
My output for the method Main directly on eclipse:
MessageCount: 4
全名: INBOX
NewMessageCount: 0
getDescription: null
getFileName: null
getMessageNumber: 1
getSize: 2297
getSentDate::2015年8月3日星期一17:23:10 BRT
getSentDate: Mon Aug 03 17:23:10 BRT 2015
来自: Silvano Wojczak silvano.wojczak@softexpertjlle.onmicrosoft.com
from: Silvano Wojczak silvano.wojczak@softexpertjlle.onmicrosoft.com
内容:javax.mail.internet.MimeMultipart@6769ba97
Content: javax.mail.internet.MimeMultipart@6769ba97
内容getClass:类javax.mail.internet.MimeMultipart
Content getClass: class javax.mail.internet.MimeMultipart
对于TomCat6中的log4j:
For log4j in TomCat6:
MessageCount: 4
全名: INBOX
NewMessageCount: 0
getDescription: null
getFileName: null
getMessageNumber: 1
getSize: 6731
getSentDate:空
发件人: null
内容:javax.mail.internet.MimeMultipart@6769ba97
Content: javax.mail.internet.MimeMultipart@6769ba97
内容getClass:类javax.mail.internet.MimeMultipart
Content getClass: class javax.mail.internet.MimeMultipart
推荐答案
在我看来,安装Tomcat 6的运行时具有自己的JavaMail实现.
It seems to me that the runtime of your installation of Tomcat 6 has its own implementation of JavaMail.
您最好先确保实际上使用了 的JavaMail.在错误发生之前,立即在读取邮件的同一程序中执行此剪贴簿:
You'd better ensure first which implementation of JavaMail are you using actually. Execute this scrap in the same program that reads the mail, immediately before the error occurs:
String packageName="javax.mail.internet.";
String simpleClassName="MimeMultipart";
String className=packageName+simpleClassName;
Class<?> cl=Class.forName(className);
URL url=cl.getResource(simpleClassName+".class");
System.out.println("url="+url);
使用较旧的geronimo-mail
实现时遇到此类问题,在读取标题之前,我先通过克隆消息来解决了这些问题:
Once I suffered this kind of problems when using older implementations of geronimo-mail
, and I got to solved them by cloning the message before reading the headers:
private static MimeMessage cloneMimeMessage(Session session, MimeMessage src)
throws MessagingException
{
if (src instanceof POP3Message)
{
return new MimeMessage(session, ((POP3Message)src).top(Integer.MAX_VALUE));
}
else if (src instanceof IMAPMessage)
{
return new MimeMessage(session, ((IMAPMessage)src).getRawInputStream());
}
else
{
throw new UnsupportedOperationException();
}
}
这篇关于Java邮件不返回主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!