赞
踩
1.pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2.application.yml
spring: mail: protocol: smtp host: smtp.qq.com username: luckymfc@qq.com #配置密码,注意不是真正的密码,而是邮箱申请到的授权码 password: nqgjgumcebsojagc # 端口号465或587 port: 587 # 默认的邮件编码为UTF-8 default-encoding: UTF-8
3.代码
@Value("${spring.mail.username}") private String from; @Autowired private JavaMailSender mailSender;public void sendMail(){ // 生成随机验证码 String code = RandomUtil.randomNumbers(4); SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo("接收者邮箱"); message.setSubject("Password Recovery"); message.setText("Your verification code is: " + code); mailSender.send(message); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。