引入jar包
java
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
java
# 发送邮件配置
mail:
smtp:
host: smtp.163.com
port: 25
auth: true
username: 139*****@163.com
password: *****
from: 139*****@163.com
name: *****
java
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;
@Component
@Slf4j
public class EmailUtil {
private static Environment environment;
@Autowired
public void setEnvironment(Environment environment) {
EmailUtil.environment = environment;
}
public static Session createSession() {
// 账号信息
String username = environment.getProperty("mail.smtp.username");// 邮箱发送账号
String password = environment.getProperty("mail.smtp.password");// 邮箱授权码
// 创建一个配置文件,并保存
Properties props = new Properties();
// SMTP服务器连接信息
props.put("mail.smtp.host", environment.getProperty("mail.smtp.host"));// SMTP主机名
// 126------25
// 163------465 163也可以用的25
props.put("mail.smtp.port", environment.getProperty("mail.smtp.port"));// 主机端口号
props.put("mail.smtp.auth", environment.getProperty("mail.smtp.auth"));// 是否需要用户认证
// props.put("mail.smtp.starttls.enale", "true");// 启用TlS加密 暂时不用
// 创建session会话
// 参数1:smtp服务器连接参数
// 参数2:账号和密码的授权认证对象
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication(username, password);
}
});
// 控制台打印调试信息
session.setDebug(true);
return session;
}
/**
* 发送纯文本邮件信息
*
* @param receiver 接收方(邮箱账号)
* @param subject 邮件主题
* @param content 邮件内容(发送内容)
*/
public void sendMessage(String receiver, String subject, String content) {
//1.创建Session
try {
Session session = createSession();
System.out.println(session);
//2.创建邮件对象
MimeMessage message = new MimeMessage(session);
//设置邮件主题
message.setSubject(subject);
//设置邮件内容
message.setText(content);
//设置发件人
String from = environment.getProperty("mail.smtp.from");
String name = environment.getProperty("mail.smtp.name");
message.setFrom(new InternetAddress(from,name));
//设置收件人
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiver));
//3.发送邮件
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 发送带附件邮件信息
*
* @param receiver 接收方(邮箱账号)
* @param subject 邮件主题
* @param content 邮件内容(发送内容)
*/
public void sendAnnexMessage(String receiver, String subject, String content,String fileName, String filePath) {
try {
//1.创建Session会话
Session session = createSession();
//2.创建邮件对象
MimeMessage message = new MimeMessage(session);
message.setSubject(subject);
//设置发件人
String from = environment.getProperty("mail.smtp.from");
String name = environment.getProperty("mail.smtp.name");
message.setFrom(new InternetAddress(from,name));
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiver));
//设置正文
BodyPart textPart = new MimeBodyPart();
//设置正文内容
textPart.setText(content);
//设置附件
BodyPart filePart = new MimeBodyPart();
filePart.setFileName(fileName);
filePart.setDataHandler(
new DataHandler(
new ByteArrayDataSource(
Files.readAllBytes(Paths.get(filePath)), "application/octet-stream")));
//整合正文和附件
Multipart part = new MimeMultipart();
part.addBodyPart(textPart);
part.addBodyPart(filePart);
//添加正文和邮件
message.setContent(part);
//3.发送邮件
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}