引言
对于某些项目来说,需要发送邮件来完成,例如,发送验证码,发送附件(文档,压缩包之类的),发送链接。本博客利用163邮箱来实现功能需求。
前期准备
修改163邮箱的POP3配置,并备份授权码,方便项目中使用
代码编写
01、pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
02、application.properties文件
# 邮箱配置
spring.mail.host=smtp.163.com
# 你的163邮箱
spring.mail.username=liuxing121380110@163.com
# 注意这里不是邮箱密码,而是SMTP授权密码
spring.mail.password=xxxxx
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
03、邮件工具类
package com.information.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail