springboot整合邮件发送(163邮箱发送为例)
//先登录163邮箱获取授权
勾选后安装提示会叫你设置授权密码之类的:记住授权的密码
1.引入maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.yml里配置: spring: mail: host: smtp.163.com //登录邮箱设置那里可以看到,第一张图所示,SMTP服务器 password: 12222 密码是第二张图设置的授权密码.不是邮箱的登录密码 username: 15902048215@163.com //这个是你的邮箱
@Service public class MailService { @Autowired private JavaMailSender mailSender; /** * 发送简单文本文件 */ public void sendSimpleEmail(){ try { SimpleMailMessage message=new SimpleMailMessage(); message.setFrom("15902048215@163.com"); message.setTo("513473567@qq.com"); message.setSubject("您有订单未支付,请按时支付"); message.setText("哈哈这是主题.............................."); mailSender.send(message); }catch (Exception e){ System.out.println("发送简单文本文件-发生异常"); } } /** * 发送html文本 * @param */ @Async public void sendHTMLMail(){ try { MimeMessage message=mailSender.createMimeMessage(); MimeMessageHelper messageHelper=new MimeMessageHelper(message,true,"utf-8"); messageHelper.setFrom("15902048215@163.com"); messageHelper.setTo("513473567@qq.com"); messageHelper.setSubject("欢迎访问"); messageHelper.setText("<a href='www.baidu.com'>百度</a>",true); mailSender.send(message); }catch (Exception e){ } } }
//调用后如图所示: