当前位置:   article > 正文

Springboot实现邮件发送_springboot开发发送邮件账号由前端填写

springboot开发发送邮件账号由前端填写

场景:用户登录时忘记密码,需要通过邮箱找回密码

1.创建springboot项目

2.pom导入依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-mail</artifactId>
  4. </dependency>

3.application.properties配置文件

  1. spring.mail.host=smtp.163.com
  2. spring.mail.username=sple_vip@163.com//发件人邮箱,需要开启smtp服务
  3. spring.mail.password=ming438434//授权密码,非邮箱的登录密码
  4. spring.mail.port=25
  5. spring.mail.protocol=smtp
  6. spring.mail.default-encoding=UTF-8

发件人邮箱的设置:

邮箱客户端授权码也需要手动开启,设置授权码

4.用户输入自己的邮箱,点击找回,前端页面

html代码:

  1. <div class="regWrap">
  2. <div class="regName">
  3. <span>请输入邮箱</span>
  4. <input id="mail" type="email" class="mail" placeholder="请输入邮箱">
  5. </div>
  6. <button id="registbtn" class="regBtn" type="button">找回</button>
  7. </div>

js代码:

  1. $(function () {
  2. $("#registbtn").click(function() {
  3. var user = {
  4. mail:$("#mail").val()
  5. }
  6. $.ajax({
  7. type: "POST",
  8. url:"/mail.action",
  9. contentType: "application/json;",
  10. data: JSON.stringify(user),
  11. success: function (data) {
  12. if(data == "successful"){
  13. alert("已发送至您的邮箱")
  14. location.href="/login.html";
  15. }
  16. if (data=="failful") {
  17. alert("邮箱错误")
  18. }
  19. },
  20. error: function (data) {
  21. alert("网络繁忙")
  22. }
  23. });
  24. });
  25. });

5.点击找回,进入controller层

  1. @RequestMapping("/mail.action")
  2. public String mail(@RequestBody User user) {
  3. return mailService.sendMail(user);
  4. }

service层

  1. @Service
  2. public class MailServiceImpl implements MailService{
  3. @Autowired
  4. private JavaMailSender javaMailSender;
  5. @Autowired
  6. private UserMapper userMapper;
  7. @Override
  8. public String sendMail(User user) {
  9. User user2 = mailPwd(user);
  10. StringBuilder stringBuilder=new StringBuilder();
  11. stringBuilder.append("<html><head><title></title></head><body>");
  12. stringBuilder.append("亲爱的用户"+user2.getAccount()+",您的当前密码是:"+user2.getPassword());
  13. stringBuilder.append("</br><a href='https://www.baidu.com/'>访问百度</a></body><html>");
  14. MimeMessage mimeMessage=javaMailSender.createMimeMessage();
  15. //multipart模式
  16. try {
  17. MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage, true);
  18. mimeMessageHelper.setTo(user.getMail());//收件人邮箱user.getMail()
  19. mimeMessageHelper.setFrom("sple_vip@163.com");//发件人邮箱
  20. mimeMessage.setSubject("找回密码");
  21. //启用html
  22. mimeMessageHelper.setText(stringBuilder.toString(),true);
  23. javaMailSender.send(mimeMessage);
  24. return Constent.SUCCESSFUL;
  25. } catch (MessagingException e) {
  26. e.printStackTrace();
  27. return Constent.FAILFUL;
  28. }
  29. }
  30. /**
  31. * 通过邮箱到数据库查询用户的信息
  32. */
  33. @Override
  34. public User mailPwd(User user) {
  35. return userMapper.mailPwd(user);
  36. }
  37. }

dao层

  1. public interface UserMapper {
  2. /**
  3. * 通过邮箱查询用户信息
  4. */
  5. @Select("SELECT * FROM user WHERE mail = #{mail}")
  6. public User mailPwd(User user);
  7. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/546047
推荐阅读
相关标签
  

闽ICP备14008679号