pom 文件
<?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>org.example</groupId>
<artifactId>email-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>1.6.7</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.33</version>
</dependency>
</dependencies>
</project>
示例代码
package org.example;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.util.Properties;
public class MailUtils {
private static final String HOST="yourmail@qq.com";
private static final String PASSWORD="password";
/**
* 发送邮件
* @param direction 邮件人邮箱地址
* @param subject 邮件名称/标题
* @param message 消息、内容
*/
public static boolean sendMail(String direction,String subject,String message){
Properties props = new Properties();
// 开启debug调试
// props.setProperty("mail.debug", "true");
// 发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件服务器主机名
props.setProperty("mail.host", "smtp.qq.com");
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
Transport transport= null;
//新建消息
Message msg = new MimeMessage(session);
try {
//设置邮件标题
msg.setSubject(subject);
//设置消息内容
msg.setText(message);
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
// 添加邮件正文
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(message, "text/html;charset=UTF-8");
multipart.addBodyPart(contentPart);
File attachment = new File("附件地址");
// 添加附件的内容
if (attachment != null) {
BodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
attachmentBodyPart.setDataHandler(new DataHandler(source));
//MimeUtility.encodeWord可以避免文件名乱码
attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
multipart.addBodyPart(attachmentBodyPart);
}
// 将multipart对象放到message中
msg.setContent(multipart);
msg.setFrom(new InternetAddress(HOST,"","UTF-8"));
transport = session.getTransport();
transport.connect("smtp.qq.com", HOST, PASSWORD);
transport.sendMessage(msg, new Address[] { new InternetAddress(direction) });
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
//关闭、释放资源
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
return true;
}
}