当前位置:   article > 正文

idea发送邮件_account.setauth(true)

account.setauth(true)

先导入依赖

  1. <dependency>
  2. <groupId>cn.hutool</groupId>
  3. <artifactId>hutool-all</artifactId>
  4. <version>5.8.4</version>
  5. </dependency>
  1. <dependency>
  2. <groupId>com.sun.mail</groupId>
  3. <artifactId>javax.mail</artifactId>
  4. <version>1.6.2</version>
  5. </dependency>

邮箱测试:

        这里使用的阿里云的邮箱,由于qq邮箱需要一个授权码,这我不知道放在哪里,后续了解到后我会来补充。

  1. @SpringBootApplication
  2. public class App {
  3. public static void main(String[] args) {
  4. SpringApplication.run(App.class, args);
  5. MailAccount account = new MailAccount();
  6. account.setHost("smtp.aliyun.com");
  7. account.setPort(25);
  8. account.setAuth(true);
  9. account.setFrom("发送的邮箱账户");
  10. //这里是发送邮箱账户
  11. account.setUser("发送的邮箱账户");
  12. //这里是发送邮箱密码
  13. account.setPass("发送的邮箱密码");
  14. //这里是需要待发送的邮箱账号
  15. ArrayList<String> toMaill = CollUtil.newArrayList("需要待发送的邮箱账号");
  16. MailUtil.send(account,toMaill , "测试", "邮件来自Hutool测试", false);
  17. }
  18. }

这个是可以发送成功的,现在我们来进行发送一个四位数的随机码来给邮箱

这里加入了随机数生成以及hutool的 StrUtil.format("您的验证码是:{}", c)字符串拼接

  1. @SpringBootApplication
  2. public class App {
  3. public static void main(String[] args) {
  4. SpringApplication.run(App.class, args);
  5. MailAccount account = new MailAccount();
  6. account.setHost("smtp.aliyun.com");
  7. account.setPort(25);
  8. account.setAuth(true);
  9. account.setFrom("发送的邮箱账户");
  10. //这里是发送邮箱账户
  11. account.setUser("发送的邮箱账户");
  12. //这里是发送邮箱密码
  13. account.setPass("发送的邮箱密码");
  14. //这里是需要待发送的邮箱账号
  15. ArrayList<String> toMaill = CollUtil.newArrayList("需要待发送的邮箱账号");
  16. int c = RandomUtil.randomInt(1000,9999);
  17. String format = StrUtil.format("您的验证码是:{}", c);
  18. MailUtil.send(account,toMaill , "验证码", format, false);
  19. }
  20. }

现在我们进行将发送邮箱的方法给抽离成service方法,这样,以后我们使用发送邮箱的时候就可以直接拷贝到我们自己的代码中直接使用。

抽离过程: 

先建立一个MailService类

加入注解@Service然后使用@Value注解将写入配置文件的数据写入

  1. @Service
  2. public class MailService {
  3. @Value("${demo.mail.host}")
  4. private String mailHost;
  5. @Value("${demo.mail.port:25}")
  6. private Integer mailPort;
  7. @Value("${demo.mail.from}")
  8. private String mailFrom;
  9. @Value("${demo.mail.user}")
  10. private String mailUser;
  11. @Value("${demo.mail.pwd}")
  12. private String mailPwd;
  13. @Value("${demo.mail.to}")
  14. private List<String> mailTo;
  15. public void send() {
  16. MailAccount account = new MailAccount();
  17. account.setHost(this.mailHost);
  18. account.setPort(this.mailPort);
  19. account.setAuth(true);
  20. account.setFrom(this.mailFrom);
  21. account.setUser(this.mailUser);
  22. account.setPass(this.mailPwd);
  23. int code = RandomUtil.randomInt(1000, 9999);
  24. String body = StrUtil.format("您的验证码为:{}",code);
  25. MailUtil.send(account, this.mailTo, "验证码", body, false);
  26. }
  27. }

配置文件

  1. demo.mail.host = smtp.aliyun.com
  2. demo.mail.port = 25
  3. demo.mail.from = 邮箱账户
  4. demo.mail.user = 邮箱账户
  5. demo.mail.pwd = 邮箱密码
  6. demo.mail.to = 待发送的邮箱账户

App调用

  1. @SpringBootApplication
  2. public class App {
  3. public static void main(String[] args) {
  4. ConfigurableApplicationContext run = SpringApplication.run(App.class, args);
  5. MailService bean = run.getBean(MailService.class);
  6. bean.send();
  7. }
  8. }

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

闽ICP备14008679号