This discussion is archived
2 Replies Latest reply: May 4, 2012 2:09 AM by 921798 RSS

Error sending email on Tomcat: javax.mail.NoSuchProviderException: smtp

921798 Newbie
Currently Being Moderated
Hello,

I have an issue regarding sending emails within a Tomcat 5.0.28 server. The exception stacktrace is the following:
2012/04/13 04:27 27 [FATAL] ro.ss.asbuilt.business.methods.StarBaseMethod.sendNotification:301 - Error sending mailsmtp
javax.mail.NoSuchProviderException: smtp
     at javax.mail.Session.getService(Session.java:750)
     at javax.mail.Session.getTransport(Session.java:689)
     at javax.mail.Session.getTransport(Session.java:632)
     at javax.mail.Session.getTransport(Session.java:612)
     at javax.mail.Session.getTransport(Session.java:667)
     at javax.mail.Transport.send0(Transport.java:154)
     at javax.mail.Transport.send(Transport.java:80)
     at ro.ss.asbuilt.business.methods.StarBaseMethod.sendNotification(StarBaseMethod.java:295)

I'm using the mail_1.4.jar 1.4 and activation_1.1.jar libraries to send email.

Above the sendNotification method code:
...
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", "" + smtpPort);
Session sessionMail = Session.getInstance(props, null);

// Construct the message
Message msg = new MimeMessage(sessionMail);
try {
msg.setFrom(new InternetAddress(from));

msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setDataHandler(new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/html")));
// Send the message
if (msg != null) {
Transport.send(msg);
log.debug("Notificare trimisa cu succes la adresa: " + to.toString());
}
} catch (AddressException e) {
log.fatal("Error sending e-mail. Address not supported! " + e.getMessage(), e);
} catch (MessagingException e) {
log.fatal("Error sending mail" + e.getMessage(), e);
}
...

Thank you.

Best regards.
  • 1. Re: Error sending email on Tomcat: javax.mail.NoSuchProviderException: smtp
    bshannon Pro
    Currently Being Moderated
    It's a Tomcat bug. Try the workarounds in this thread:
    Fetching email with attachments in Tomcat
  • 2. Re: Error sending email on Tomcat: javax.mail.NoSuchProviderException: smtp
    921798 Newbie
    Currently Being Moderated
    Hello,

    Thank you for your quick reply.

    After I tried the solutions specified in the thread you mentioned it (Solution 1. Copy the mail.jar and activation.jar to <tomcat>/common/lib folder;
    Solution 2. Within sending email method I added: Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader() ) I received another error:

    java.lang.NoSuchMethodError: com.sun.mail.util.SocketFetcher.getSocket(Ljava/lang/String;ILjava/util/Properties;Ljava/lang/String;Z)Ljava/net/Socket;
         at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
         at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
         at javax.mail.Service.connect(Service.java:233)
         at javax.mail.Service.connect(Service.java:134)
         at javax.mail.Service.connect(Service.java:86)
         at javax.mail.Transport.send0(Transport.java:162)
         at javax.mail.Transport.send(Transport.java:80)

    This was happening because one of the jar files that tomcat had in its classpath referred another mail.jar (in its manifest file) that was older than mine.

    So, I reverted any changes I had made on first step (the solutions 1,2) and I fixed it only by setting my mail.jar as first jar within tomcat classpath.

    Best regards.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points