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

Zer*_*ees 3 jakarta-mail intellij-idea

我在我的项目中使用 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 设置上。请告诉我是否可以解决此问题。

在此输入图像描述

ega*_*rdo 6

试试这个 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>
Run Code Online (Sandbox Code Playgroud)

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

您还可以使用较新版本的 java mail 依赖项,例如 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>
Run Code Online (Sandbox Code Playgroud)

根据您的代码,我创建了一个简化的项目版本,只有两个文件;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>
Run Code Online (Sandbox Code Playgroud)

和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();
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 确保您的“java”源文件夹被标记为源(右键单击它并选择“将目录标记为 -> 源根目录”,如果它还不是浅蓝色)
  • 确保类包 (com.test) 名称匹配,即项目窗格上的“src/main/java/com/test/SendMail”和 SendMail.java 中的“package com.test”

IDEA 项目显示包和外部库