赞
踩
邮件发送的基本过程与概念
邮件传输协议
项目中添加依赖
<!--发送邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
spring:
# 邮箱服务配置
mail:
host: smtp.163.com
username: 18811112222@163.com
password: 授权码
from: 18811112222@163.com
default-encoding: utf-8
protocol: smtps
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true
微服务service封装
package com.gen.service.impl; import com.gen.service.MailService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; /** * 邮件发送服务 */ @Service @Slf4j public class MailServiceImpl implements MailService { @Autowired private JavaMailSender mailSender; @Value("${spring.mail.from}") private String from; /** * 发送简单邮件 * * @param to 收信人 * @param subject 主题 * @param content 正文 */ @Override public void sendSimpleMail(String to, String subject, String content) { // 创建SimpleMailMessage对象 SimpleMailMessage message = new SimpleMailMessage(); // 邮件发送人 message.setFrom(from); // 邮件接收人 message.setTo(to); // 邮件主题 message.setSubject(subject); // 邮件内容 message.setText(content); // 发送邮件 this.mailSender.send(message); log.info("邮件发送成功===》{}",message); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。