Search code examples
javajakarta-mailtomcat6

Java mail don't return subject


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.

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).

Thanks for help

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());
}

My output for the method Main directly on eclipse:

MessageCount: 4

full name: INBOX

NewMessageCount: 0

getDescription: null

getFileName: null

getMessageNumber: 1

getSize: 2297

getSentDate: Mon Aug 03 17:23:10 BRT 2015

from: Silvano Wojczak silvano.wojczak@softexpertjlle.onmicrosoft.com

Content: javax.mail.internet.MimeMultipart@6769ba97

Content getClass: class javax.mail.internet.MimeMultipart

For log4j in TomCat6:

MessageCount: 4

full name: INBOX

NewMessageCount: 0

getDescription: null

getFileName: null

getMessageNumber: 1

getSize: 6731

getSentDate: null

from: null

Content: javax.mail.internet.MimeMultipart@6769ba97

Content getClass: class javax.mail.internet.MimeMultipart


Solution

  • It seems to me that the runtime of your installation of Tomcat 6 has its own implementation of 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);
    

    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();
        }
    }