java.lang.NoSuchMethodError: com.sun.mail.util.TraceInputStream
Asked Answered
A

5

8

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?

Asci answered 10/5, 2015 at 19:58 Comment(1)
Similar and in my case more useful question/answers: #45444531Thighbone
C
6

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());
Casta answered 10/5, 2015 at 20:17 Comment(5)
where should this line of code go? globally or just when send an email?Confession
This chunk of code is useful to understand which version of smtp library you are using, so put it wherever you want in the program to see which version of the library you loaded ... You can even put it into the main method, it is just for debugging .Casta
It always reruns null no matter what Mail live I have in my classpathConfession
so you don't have the class TraceInputStream inside the library that you put in the class path...Casta
i was getting NoSuchMethodError: com.sun.mail.util.LineOutputStream also due to missing jar mvnrepository.com/artifact/com.sun.mail/smtp/1.6.3, just added the maven dependancy fixed itLyophilic
A
6

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>
Asa answered 21/12, 2020 at 12:4 Comment(1)
not sure about the <artifactId>smtp</artifactId> i added the below dependencie ` implementation 'javax.mail:javax.mail-api:1.4.4'` ` implementation 'javax.mail:mail:1.4.4'`Extrusion
C
3

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.

Colossus answered 11/5, 2015 at 3:29 Comment(0)
E
0

Getting reference from @Osanda Deshan. Adding below gradle dependencies Worked for me.

    implementation 'javax.mail:javax.mail-api:1.4.4'
    implementation 'javax.mail:mail:1.4.4'
Extrusion answered 15/12, 2022 at 14:45 Comment(0)
C
0

Use the same version number both jakarta.mail-api and jakarta.mail to avoid this issue.

<dependency>
    <groupId>jakarta.mail</groupId>
    <artifactId>jakarta.mail-api</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>jakarta.mail</artifactId>
    <version>2.0.1</version>
</dependency>
Clementia answered 9/7 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.