//先登录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: [email protected] //这个是你的邮箱
@Service public class MailService { @Autowired private JavaMailSender mailSender; /** * 发送简单文本文件 */ public void sendSimpleEmail(){ try { SimpleMailMessage message=new SimpleMailMessage(); message.setFrom("[email protected]"); message.setTo("[email protected]"); 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("[email protected]"); messageHelper.setTo("[email protected]"); messageHelper.setSubject("欢迎访问"); messageHelper.setText("<a href=‘www.baidu.com‘>百度</a>",true); mailSender.send(message); }catch (Exception e){ } } }
//调用后如图所示:
原文地址:https://www.cnblogs.com/yangxiaohui227/p/11332069.html
时间: 2024-10-13 10:59:19