我在我的项目中使用javax.mail库。我的项目使用-mvn clean install正常构建,但是当我尝试调试我的Intellij IDE时显示错误并且无法识别javax.mail导入。我已经从FILE重新启动了我的IDE - > Invalidate Caches并重启,仍然没有运气。
这些没有被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坐标已更改为雅加达:
<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 = "[email protected]";
String from = "[email protected]";
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();
}
}