发送邮件功能
- 一.pom.xml文件添加邮件依赖
- 二.发送邮件信息
- (1)固定配置在application.yml
- (2)发送邮箱配成活
- (3)底层发送邮件方法
- (4)QQ邮箱开通smtp服务
- (5)网易邮箱开通smtp服务
一.pom.xml文件添加邮件依赖
<!-- 发送邮件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
二.发送邮件信息
有两种方式配置:固定配置在application.yml中,或者将发送邮箱配成活的参数
注意:发送者必须开通了smtp服务,(4)和(5)目录中介绍开通smtp服务
备注:host配置
网易邮箱:smtp.163.com
QQ邮箱:smtp.qq.com
(1)固定配置在application.yml
在application.yml中配置
spring:mail:host: smtp.163.com #邮件服务器地址port: 465 #端口号username: 邮箱地址password: 开通smtp服务获取的授权码default-encoding: UTF-8 #默认编码格式properties:mail:debug: true #启动debug调试smtp:auth: truessl:enable: truestarttls:enable: truerequired: true
发送邮件接口
@RequiredArgsConstructor
@RestController
public class SendEmailController
{
private final JavaMailSender mailSender;
// 发送邮件接口
public void sendEmail() {try {SendMail.sendEmail(mailSender, username, dto.getContent(), dto.getFileName(), dto.getReUserMail());} catch (Exception e) {e.printStackTrace();}}
}
(2)发送邮箱配成活
不需要在application.yml中配置信息
public class SendEmailController {public void sendEmail() {// 邮箱配置String host = "smtp.163.com";Integer port = 465;String username = "邮箱";String password = "授权密码";String content = "发送内容";String fileName = "发送主题";String reUserMail = "收件人";try {SendMail.sendEmail(host, port, username, password, content, fileName, reUserMail);} catch (Exception e) {e.printStackTrace();}}
}
(3)底层发送邮件方法
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.internet.*;
import java.util.Properties;@Slf4j
public class SendMail {/**** @param host 邮件服务器地址* @param port 端口号* @param username 发送者邮箱* @param password 授权密码* @param content 内容* @param fileName 主题* @param reUserMail 收件人邮箱*/public static void sendEmail(String host, int port, String username, String password, String content, String fileName, String reUserMail){JavaMailSenderImpl mailSender = new JavaMailSenderImpl();mailSender.setHost(host);mailSender.setPort(port);mailSender.setUsername(username);mailSender.setPassword(password);mailSender.setDefaultEncoding("utf-8");Properties javaMailProperties = mailSender.getJavaMailProperties();javaMailProperties.setProperty("mail.smtp.auth", "true");javaMailProperties.setProperty("mail.smtp.ssl.enable", "true");javaMailProperties.setProperty("mail.smtp.starttls.enable", "true");javaMailProperties.setProperty("mail.smtp.starttls.required", "true");javaMailProperties.setProperty("mail.debug", "true");//启用调试javaMailProperties.setProperty("mail.smtp.timeout", "3000"); //设置超时时间3秒mailSender.setJavaMailProperties(javaMailProperties);sendEmail(mailSender, username, content, fileName, reUserMail);}/*** 发送邮件内容* @param mailSender* @param sender 发送人邮箱* @param content 发送内容* @param fileName 主题* @param reUserMail 收件人邮箱*/public static void sendEmail(JavaMailSender mailSender, String sender, String content, String fileName, String reUserMail) {MimeMessage message = null;try {message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);/**发送者邮箱,即为开通了smtp服务的邮箱*/helper.setFrom(sender);/**发送到的邮箱*/helper.setTo(reUserMail);helper.setSubject("主题:" + fileName);helper.setText("带附件的邮件内容");helper.setText(content, true);mailSender.send(message);} catch (Exception e){e.printStackTrace();}}}
(4)QQ邮箱开通smtp服务
在邮箱->设置->账号->开通POP3/SMTP服务
(5)网易邮箱开通smtp服务
邮箱->设置->POP3/SMTP/IMAP->开通POP3/SMTP和IMAP/SMTP服务