我正在使用javamail来救收收件箱的邮件。我从收件箱中收到所有邮件,但主题(message.getSubject())返回null。
我在几个网站上进行研究,所有提出的解决方案都不起作用。
这个问题只发生在Tomcat6中,如果在main方法的java项目中执行偶数代码我得到所有的atributis,包括subject。我是用户de SSL协议(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());
}
方法Main的输出直接在eclipse:
MessageCount: 4
全名: INBOX
NewMessageCount: 0
getDescription: null
getFileName: null
getMessageNumber: 1
getSize: 2297
getSentDate: 2015年8月3日星期一17:23:10 BRT 2015
来自: Silvano Wojczak silvano.wojczak@softexpertjlle.onmicrosoft.com
内容: javax.mail.internet.MimeMultipart@6769ba97
内容getClass:类javax.mail.internet.MimeMultipart
对于TomCat6中的log4j:
MessageCount: 4
全名: INBOX
NewMessageCount: 0
getDescription: null
getFileName: null
getMessageNumber: 1
getSize: 6731
getSentDate: null
来自: null
内容: javax.mail.internet.MimeMultipart@6769ba97
内容getClass:类javax.mail.internet.MimeMultipart
答案 0 :(得分:2)
在我看来,安装Tomcat 6的运行时有自己的JavaMail实现。
您最好先确保使用实际的JavaMail实现。在错误发生之前立即在读取邮件的同一程序中执行此废料:
geronimo-mail
在使用较早的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();
}
}
实现时遇到此类问题,我在阅读标题之前通过克隆解决了这些问题:
what2do=${0##*/}
what2do=${what2do%.sh}
doAddRepository () {
local repoName="$1"
# Adding new repository with the name $repoName...
}
doAddProject () {
local projName="$1"
# Adding new project with the name $projName...
}
doAddUser () {
local userName="$1"
# Some code to add user "$userName"...
}
case $what2do in
addRepository)
while getopts 'n:' key; do
case $key in
n) REPO_NAME=$OPTARG ;;
...
esac
done
doAddRepository "$REPO_NAME" "$SOME_OTHER_ARG"
exit $?
;;
addProject)
while getopts 'n:' key; do
case $key in
n) PROJ_NAME=$OPTARG ;;
...
esac
done
doAddProject "$PROJ_NAME" "$SOME_OTHER_ARG"
exit $?
;;
addUser)
while getopts 'u:' key; do
case $key in
u) USER_NAME=$OPTARG ;;
...
esac
done
doAddUser "$USER_NAME" "$SOME_OTHER_ARG"
exit $?
;;
*)
echo "I don't know how to $what2do. Maybe, it is NIY" >&2
doShowUsage
exit 1
;;
esac