Question

I just started out using JavaMail and I'm having difficulty making the e-mail display a few things. The messages get sent and received, however, when it comes up the subject and to: lines are empty.

This is the function I'm trying to send e-mail with. I didn't configure any properties so everything should be going at their default.

public void sendEmail(String[] ToEmailAddr, String Subject, String Body){

  Session session = Session.getDefaultInstance( fMailServerConfig, null );
  MimeMessage message = new MimeMessage( session );
  try {
    for (int i=0;i<ToEmailAddr.length;i++) {
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(ToEmailAddr[i]));
    }
    message.setSubject( Subject );
    message.setText( Body );
    Transport.send( message );
  }
  catch (MessagingException ex){
    logger.error("Cannot send email. " + ex);
  }

}

How can I get the recipient to see the list of recipients and the subject line?

Was it helpful?

Solution

Turns out there was a conflict in packages. Tomcat automatically includes its own JavaMail package in the Maven build for the web project which was causing problems when I was importing from the standard JavaMail.

In the project's pom.xml file, I had to exclude geronimo-javamail like this:

    <dependency>
        <groupId>org.apache.axis2</groupId>
        <artifactId>axis2-xmlbeans</artifactId>
        <version>1.4.1</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.geronimo.specs</groupId>
                <artifactId>geronimo-javamail_1.4_spec</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

OTHER TIPS

This Happens because Axis2 have geronimo-javamail_1.4_spec-1.2.jar file inside its lib , which is having javax.mail packages with it, a very simple solution will be open-up the jar file and delete the package inside the geronimo jar file , and add javax.mail packages from the oracle-sun downloaded lib to the class-path of the app

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top