Search code examples
javamavenjakarta-mailclassnotfoundexceptionmaven-dependency-plugin

Maven searching for an old version of Javax.Mail dependency (1.4.3) though I have the latest version of it (1.6.1) and throwing ClasNotFoundException


I am trying to write a piece of code in the Cucumber testRunner class, to send the test result report through Email after execution, but I have come across this issue that I have not been able to solve. I added the dependency for the Javax.Mail libraries (version 1.6.1) and wrote the following methods inside testRunner,

@AfterClass
public static void teardown() throws UnknownHostException, IOException {       
    HTML_TestExecutionReportParser testExecutionStatusParser = new HTML_TestExecutionReportParser();
    sendTestExecutionReportEmail(testExecutionStatusParser.isAssertionFailureFound());    

}

public static void sendTestExecutionReportEmail(boolean testExecutionStatus) throws IOException {
    String mailSubject = null;
    String mailBody = null;     
    if (testExecutionStatus) {
        mailSubject = "Sub";
        mailBody = "body";
        sendEmail(mailSubject,mailBody);            
    }
    else {
        mailSubject = "Sub";
        mailBody = "body";          
        sendEmail(mailSubject,mailBody);
    }

}

public static void sendEmail(String mailSubject, String mailBody) throws IOException {
    Properties props = new Properties();
    Properties prop = new Properties();
    PropertyFileLoader propFileLoader = new PropertyFileLoader();
    prop = propFileLoader.loadPropertyFile("PropertyFiles", "EmailConfiguration.properties");
    props.put("mail.smtp.host", prop.getProperty("SMTPHostName"));
    props.put("mail.smtp.port", prop.getProperty("SMTPPort"));
    props.put("mail.smtp.ssl.trust", prop.getProperty("SMTPHostName"));
    final String EmailP = prop.getProperty("SMTPP");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.connectiontimeout", "10000");
    final String User = prop.getProperty("SMTPU");
    Configurations config = new Configurations();
    String reportFolderPath = "TestExecutionReports";
    String reportName = "report.html";

    try {
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(User, EmailP);
            }
        });

        InternetAddress fromAddress = new InternetAddress("email", "name");
        InternetAddress toAddress = new InternetAddress("email", "name");
        session.getProviders();
        session.setDebug(true);
        Message msg = new MimeMessage(session);
        msg.setFrom(fromAddress);
        msg.addRecipient(Message.RecipientType.TO, toAddress);
        msg.setSubject(mailSubject);
        Multipart multipart = new MimeMultipart();
        MimeBodyPart textBodyPart = new MimeBodyPart();
        textBodyPart.setText(mailBody);
        MimeBodyPart attachmentBodyPart = new MimeBodyPart();
        String attachmentName = config.getResourcePath(reportFolderPath) + reportName;
        DataSource source = new FileDataSource(attachmentName);
        attachmentBodyPart.setDataHandler(new DataHandler(source));
        attachmentBodyPart.setFileName("Report.html");
        multipart.addBodyPart(textBodyPart);
        msg.setContent(multipart);      
        Transport.send(msg);

    } catch (MessagingException e) {
        System.out.println(e.getMessage() + e.getStackTrace());
    } catch (UnsupportedEncodingException e) {
        System.out.println(e.getMessage());
    }
}

There are no errors shown in the code, but when I run it, it throws the following error in the console,

DEBUG: setDebug: JavaMail version 1.4.3
DEBUG: getProvider() returning 
javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun 
Microsystems, Inc.,1.4.3]
java.lang.ClassNotFoundException: com.sun.mail.smtp.SMTPTransport
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)

Initially when I wrote the code, although I had the dependency with the latest version mentioned in the POM.xml file, I couldn't find any Javax.mail jar file under the Maven Dependencies, but the code wasn't showing any error. Later I somehow managed to properly install the Javax.mail libraries (version 1.6.1) through Maven, but when I run the tests, the above exception kept throwing and from the first line of the above console output, I am assuming that Maven is looking for an older version of Javax.mail libraries and since it is not available, it is throwing the classNotFound exception. But I am not sure how to point Maven to the latest Javax.mail libraries that is available under Maven Dependencies.

Any help is much appreciated...!

Edit: The mvn dependency:tree log as below,

 RestAssured:APITesting:jar:0.0.1-SNAPSHOT
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.0:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson- 
annotations:jar:2.9.0:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.0:compile
[INFO] +- io.rest-assured:rest-assured:jar:3.0.7:test
[INFO] |  +- org.codehaus.groovy:groovy:jar:2.4.12:test
[INFO] |  +- org.codehaus.groovy:groovy-xml:jar:2.4.12:test
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.5.3:compile
[INFO] |  |  +- org.apache.httpcomponents:httpcore:jar:4.4.6:compile
[INFO] |  |  +- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  |  \- commons-codec:commons-codec:jar:1.9:compile
[INFO] |  +- org.apache.httpcomponents:httpmime:jar:4.5.1:compile
[INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] |  +- org.ccil.cowan.tagsoup:tagsoup:jar:1.2.1:test
[INFO] |  +- io.rest-assured:json-path:jar:3.0.7:test
[INFO] |  |  +- org.codehaus.groovy:groovy-json:jar:2.4.12:test
[INFO] |  |  \- io.rest-assured:rest-assured-common:jar:3.0.7:test
[INFO] |  \- io.rest-assured:xml-path:jar:3.0.7:test
[INFO] |     \- javax.xml.bind:jaxb-api:jar:2.2.12:test
[INFO] +- junit:junit:jar:4.12:compile
[INFO] +- io.rest-assured:json-schema-validator:jar:3.0.3:test
[INFO] |  \- com.github.fge:json-schema-validator:jar:2.2.6:test
[INFO] |     +- com.google.code.findbugs:jsr305:jar:3.0.0:test
[INFO] |     +- joda-time:joda-time:jar:2.3:test
[INFO] |     +- com.googlecode.libphonenumber:libphonenumber:jar:6.2:test
[INFO] |     +- com.github.fge:json-schema-core:jar:1.2.5:test
[INFO] |     |  +- com.github.fge:uri-template:jar:0.9:test
[INFO] |     |  |  +- com.github.fge:msg-simple:jar:1.1:test
[INFO] |     |  |  |  \- com.github.fge:btf:jar:1.2:test
[INFO] |     |  |  \- com.google.guava:guava:jar:16.0.1:test
[INFO] |     |  +- com.github.fge:jackson-coreutils:jar:1.8:test
[INFO] |     |  \- org.mozilla:rhino:jar:1.7R4:test
[INFO] |     +- javax.mail:mailapi:jar:1.4.3:test
[INFO] |     \- net.sf.jopt-simple:jopt-simple:jar:4.6:test
[INFO] +- info.cukes:cucumber-java:jar:1.2.5:test
[INFO] |  \- info.cukes:cucumber-core:jar:1.2.5:test
[INFO] |     +- info.cukes:cucumber-html:jar:0.2.3:test
[INFO] |     +- info.cukes:cucumber-jvm-deps:jar:1.0.5:test
[INFO] |     \- info.cukes:gherkin:jar:2.12.2:test
[INFO] +- info.cukes:cucumber-junit:jar:1.2.5:test
[INFO] +- com.google.code.gson:gson:jar:2.3.1:compile
[INFO] +- org.assertj:assertj-core:jar:3.5.1:test
[INFO] +- com.vimalselvam:cucumber-extentsreport:jar:3.0.0:compile
[INFO] +- com.aventstack:extentreports:jar:3.0.6:compile
[INFO] |  +- org.freemarker:freemarker:jar:2.3.23:compile
[INFO] |  +- org.mongodb:mongodb-driver:jar:3.3.0:compile
[INFO] |  |  +- org.mongodb:bson:jar:3.3.0:compile
[INFO] |  |  \- org.mongodb:mongodb-driver-core:jar:3.3.0:compile
[INFO] |  \- org.jsoup:jsoup:jar:1.9.2:compile
[INFO] +- com.github.javafaker:javafaker:jar:0.12:compile
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.5:compile
[INFO] |  \- com.github.mifmif:generex:jar:1.0.2:compile
[INFO] |     \- dk.brics.automaton:automaton:jar:1.11-8:compile
[INFO] +- javax.mail:javax.mail-api:jar:1.6.1:compile
[INFO] |  \- javax.activation:activation:jar:1.1:compile
[INFO] \- log4j:log4j:jar:1.2.17:compile

Solution

  • You need to exclude javax.mail:mailapi from io.rest-assured:json-schema-validator.

    <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>json-schema-validator</artifactId>
      <version>3.0.3</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>javax.mail</groupId>
          <artifactId>mailapi</artifactId>
        </exclusion>
      </exclusions> 
    </dependency>
    

    The problem is that your pom.xml effectively includes two Java Mail API artifacts: javax.mail:mailapi and javax.mail:javax.mail-api. Technically these are two different artifacts therefor including javax.mail-api with specific version does not override the version of mailapi.

    Anyway mailapi seems to be the invalid artifact, probably deployed by someone by mistake.

    You might also want to contact developers of json-schema-validator and let them know that they use the invalid mailapi artifact.