I'm trying to send an email through Java Mail API and it works fine on my laptop. When I'm doing exactly the same in Heroku, I'm getting this:
java.lang.NoSuchMethodError: com.sun.mail.util.TraceInputStream.(Ljava/io/InputStream;Lcom/sun/mail/util/MailLogger;)V
at com.sun.mail.smtp.SMTPTransport.initStreams(SMTPTransport.java:2014)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1936)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:291)
at ...
Here is what I have in pom.xml
:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mailapi</artifactId>
<version>1.4.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.3</version>
<scope>runtime</scope>
</dependency>
I guess there is another version of Java Mail API inside Heroku JDK, which doesn't have this constructor... How can this be fixed?
By default, Java apps running on latest Heroku stack use OpenJDK 8.
Your problem does not seems related to the actual JVM implementation but rather due to the missing smtp-1.5.1.jar
in classpath . To be sure to load correctly TraceInputStream try this :
java.net.URL classUrl = this.getClass().getResource("com.sun.mail.util.TraceInputStream");
out.println(classUrl.getFile());
I also faced the same issue. It was because I don't have the class TraceInputStream inside the library. I simply downgrade libraries to 1.4.4 and it worked.
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>smtp</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>mailapi</artifactId>
<version>1.4.4</version>
</dependency>
You've mixed different versions of the API and implementation; don't do that. For that matter, you only need the com.sun.mail:javax.mail dependency. If Heroku isn't providing it in the runtime environment, you'll need to package it in your application. Make sure the JavaMail jar file is ending up in the WEB-INF/lib directory of your application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With