Intellij无法识别javax.mail导入,但项目正在构建中

时间:2019-04-23 09:37:32

标签: intellij-idea javamail

我在我的项目中使用javax.mail库。我的项目使用-mvn clean install构建良好,但是当我尝试调试Intellij IDE时显示错误,并且无法识别javax.mail导入。我已经从FILE-> Invalidate Caches重新启动了IDE,然后重新启动,仍然没有运气。

这些没有被intellij IDEA识别,指出了未使用的进口。我在下面的独立性版本中使用:-javax.activation-1.1.1和javax.mail-1.4。

由于项目进展顺利,我认为问题出在某些IDE设置上。请告诉我是否可以解决此问题。

enter image description here

1 个答案:

答案 0 :(得分:2)

尝试以下Maven依赖项:

<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>

并且您还应该在外部库下看到邮件类-> Maven:javax.mail:mail:1.4-> mail-1.4.jar-> javax.mail

您还可以使用Java邮件依赖项的较新版本,例如1.4.7或1.5.0-b01

最新版本(由@Mark Rotteveel指出)是1.6.3,并且maven坐标已更改为jakarta:

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

根据您的代码,我创建了一个仅包含两个文件的简化项目版本; 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>

和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();
    }
}
  • 确保将“ java”源文件夹标记为源(右键单击并选择“将目录标记为->源根目录”(如果尚未为浅蓝色))
  • 确保类包(com.test)的名称与项目窗格上的“ src / main / java / com / test / SendMail”匹配,而与SendMail.java中的“包com.test”相匹配

IDEA Project showing package and External Libraries