繁体   English   中英

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

[英]Intellij not recognizing javax.mail imports but project is building fine

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

在此处输入图片说明

尝试以下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中匹配“ package com.test”

IDEA项目显示软件包和外部库

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM