当前位置:   article > 正文

java 用163邮箱发送邮件_163邮件发送 java

163邮件发送 java

maven 配置

  1. <dependency>
  2. <groupId>cn.hutool</groupId>
  3. <artifactId>hutool-all</artifactId>
  4. <version>5.8.26</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.sun.mail</groupId>
  8. <artifactId>javax.mail</artifactId>
  9. <version>1.6.2</version>
  10. </dependency>

java 代码

  1. package com.bx.api.common.util;
  2. import cn.hutool.core.util.StrUtil;
  3. import cn.hutool.extra.mail.MailAccount;
  4. import cn.hutool.extra.mail.MailUtil;
  5. public class EmailSenderUtil {
  6. private static final String SMTP_HOST = "smtp.163.com";
  7. private static final int SMTP_PORT = 25;
  8. private static final String EMAIL_USERNAME = "1111a@163.com";
  9. private static final String EMAIL_PASSWORD = "1111MI";
  10. public static void sendEmail(String to, String subject, String content) {
  11. if (StrUtil.isBlank(to)) {
  12. System.err.println("收件人不能为空");
  13. return;
  14. }
  15. if (StrUtil.isBlank(subject)) {
  16. System.err.println("邮件主题不能为空");
  17. return;
  18. }
  19. if (StrUtil.isBlank(content)) {
  20. System.err.println("邮件内容不能为空");
  21. return;
  22. }
  23. MailAccount account = new MailAccount();
  24. account.setHost(SMTP_HOST);
  25. account.setPort(SMTP_PORT);
  26. account.setAuth(true);
  27. account.setFrom(EMAIL_USERNAME);
  28. account.setUser(EMAIL_USERNAME);
  29. account.setPass(EMAIL_PASSWORD);
  30. try {
  31. MailUtil.send(account, to, subject, content, false);
  32. System.out.println("邮件发送成功");
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. System.out.println("邮件发送失败");
  36. }
  37. }
  38. public static void main(String[] args) {
  39. String to = "1111@qq.com";
  40. String subject = "测试邮件";
  41. String content = "这是一封测试邮件";
  42. sendEmail(to, subject, content);
  43. }
  44. }

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

闽ICP备14008679号