用Java发送邮件:服务接口供其他业务调用
最编程
2024-02-13 21:55:43
...
相关依赖
<!-- JavaMail API -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<!-- Spring Boot Mail Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
接口设计
package cn.tedu.tea.admin.server.email.service;
public interface IEmailService {
// 收件人邮箱 邮箱标题 消息体
void sendEmail(String to, String subject, String content);
}
接口实现类
package cn.tedu.tea.admin.server.email.service.impl;
import cn.tedu.tea.admin.server.email.service.IEmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
@Slf4j
@Service
public class EmailServiceImpl implements IEmailService {
public EmailServiceImpl() {
log.debug("创建发送邮件的业务对象: EmailServiceImpl");
}
// 邮件服务器配置
@Value("${spring.mail.host}")
private String host;
@Value("${spring.mail.port}")
private int port;
@Value("${spring.mail.username}")
private String username;
@Value("${spring.mail.password}")
private String password;
@Value("${spring.mail.properties.mail.smtp.auth}")
private boolean auth;
@Value("${spring.mail.properties.mail.smtp.starttls.enable}")
private boolean enable;
public void sendEmail(String to, String subject, String content) {
// 创建Properties类用于记录邮箱的一些属性
Properties props = new Properties();
// 表示SMTP发送邮件,需要进行身份验证
props.put("mail.smtp.auth", auth);
// 此处填写SMTP服务器
props.put("mail.smtp.host", host);
// 端口号,QQ邮箱端口587
props.put("mail.smtp.port", port);
// 此处填写发送邮件的账号
props.put("mail.user", username);
// QQ邮箱生成的授权码
props.put("mail.password", password);
// 启用STARTTLS加密,设置为true表示启用该加密方式
props.put("mail.smtp.starttls.enable", enable);
Authenticator auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(props.getProperty("mail.user"), props.getProperty("mail.password"));
}
};
// 创建邮件会话
Session session = Session.getInstance(props, auth);
try {
// 创建邮件消息
Message message = new MimeMessage(session);
// 设置发件人
InternetAddress from = new InternetAddress(props.getProperty("mail.user"));
message.setFrom(from);
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
// 设置邮件标题
message.setSubject(subject);
// 设置邮件的内容体
message.setContent(content, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
log.debug("邮件发送成功");
} catch (MessagingException e) {
log.error("邮件发送失败" + e.getMessage());
}
}
}
配置文件
# Spring的配置
spring:
# 邮箱的配置
mail:
# 邮件服务器的主机名
host: smtp.qq.com(这里用的是qq的其他的自行查找)
# 邮件服务器的端口号
port: 587
# 发件人的邮箱用户名
username: 发件的邮箱
# 发件人的邮箱密码或授权码
password: 授权码不是密码 示例(fvfgddauqttdsbei)
properties:
mail:
smtp:
# 指定是否需要进行身份验证,设置为true表示需要身份验证
auth: true
starttls:
# 启用STARTTLS加密,设置为true表示启用该加密方式
enable: true
上一篇: 理解并正确使用标签
下一篇: 对比分析国内八大域名注册商的优缺点