Springboot后台增加邮箱服务,以QQ邮箱为例

一、添加pom依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

二、application.properties配置

#邮箱服务
spring.mail.host=smtp.qq.com
[email protected]
#该密码为qq邮箱开启POP3/SMTP之后生成的授权码
spring.mail.password=xxxxx
spring.mail.default-encoding=utf-8
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
#自定义变量
[email protected]

三、编写Service

@Service
public class MailServiceImpl implements MailService {
    @Autowired
    private JavaMailSender mailSender;
    @Value("${MAIL_SENDER}")
    private String from;

    /***
     * 邮箱发件服务
     * @param to  收件人邮件
     * @param subject  主题
     * @param content  内容
     */
    @Override
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        try{
            mailSender.send(simpleMailMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
发布了25 篇原创文章 · 获赞 0 · 访问量 1482

猜你喜欢

转载自blog.csdn.net/u014681799/article/details/104534989