2

I am using javax.mail library in my project. My project is building fine using - mvn clean install, but when i try to debug my Intellij IDE shows error and it is not able to recognize the javax.mail imports. I have restarted my IDE from FILE -> Invalidate Caches and restart,still no luck.

These are not getting recognized by intellij IDEA,stating unused imports. I am using below the depeendency versions as:- javax.activation - 1.1.1 and javax.mail - 1.4.

Because the project is building fine,i believe the problem lies in some IDE setting.Kindly let me know if this can be resolved.

enter image description here

1
  • 1
    The circle over your external libraries makes no sense, as it doesn't show any javax.mail there... Apr 23, 2019 at 14:37

1 Answer 1

5

Try this maven dependency:

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4</version>
    <scope>provided</scope> <!-- add this only if code will run in a java container (i.e. tomcat, etc)-->
</dependency>

And you should also see the mail classes under External Libraries -> Maven:javax.mail:mail:1.4 -> mail-1.4.jar -> javax.mail

There are also newer versions of the java mail dependency you can use, like 1.4.7 or 1.5.0-b01

The latest version (as pointed out by @Mark Rotteveel) is 1.6.3 and the maven coordinates have changed to jakarta:

    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>jakarta.mail</artifactId>
        <version>1.6.3</version>
    </dependency>

Based on your code I have created a simplified project version with only two files; pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>message-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>
    </dependencies>
</project>

and SendMail.java

package com.test;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Properties;

public class SendMail {
    public static void main(String[] args) {
        sendMail(new Exception("Problem with cable"));
    }

    public static void sendMail(Exception exception) {
        String to = "destination@test.com";
        String from = "sender@test.com";
        String host = "smtp.test.com";
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", host);
        Session session = Session.getDefaultInstance(properties);
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("Trade-processor instance shutdown!");
            message.setText(getExceptionMessage(exception));
            Transport.send(message);
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }

    private static String getExceptionMessage(Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        return sw.toString();
    }
}
  • Make sure your 'java' source folder is marked as source (right click on it and select 'Mark directory as -> sources root' if it is not already light blue)
  • Make sure the class package (com.test) names match i.e. 'src/main/java/com/test/SendMail' on the project pane and 'package com.test' in SendMail.java

IDEA Project showing package and External Libraries

4
  • 1
    Latest version is 1.6.3, see: eclipse-ee4j.github.io/javamail And using provided scope is only correct if you are going to deploy to a JavaEE server that includes JavaMail. Apr 23, 2019 at 14:38
  • I provided this <scope>provided</scope> and it worked.Thanks !
    – ZerekSees
    Apr 24, 2019 at 6:38
  • @egallardo Now the code is not showing any errors,but when i try to debug,it is running into ClassNotFoundException: javax.mail.MessagingException,Is there any way to resolve this also?
    – ZerekSees
    Apr 24, 2019 at 8:24
  • 1
    @ZerekSees Remove the <scope>provided</scope> line. Sounds contradictory but it looks like the problem was somewhere else.
    – egallardo
    Apr 24, 2019 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.