当前位置:   article > 正文

提供两个发送短信和发送邮件的工具类,开箱即用_sendsmsutil

sendsmsutil

        前言:现在基本都是SpringBoot开发项目,所以要先在yml文件配置一些密钥之类的;

  1. email:
  2. emailFrom: 22**5@qq.com
  3. host: smtp.**com
  4. authorizationCode: *******
  5. emailContent:
  6. emailTo: lwd****06@163.com
  7. sendSMS:
  8. signName: **
  9. endpoint: ******
  10. templateCode: *****
  11. accessKeyId: ***********
  12. accessKeySecret: ******************

一.发送短信SMSUtil

  1. @Slf4j
  2. @Component
  3. public class SMSUtil {
  4. @Value("${sendSMS.accessKeyId}")
  5. private String accessKeyId;
  6. @Value("${sendSMS.accessKeySecret}")
  7. private String accessKeySecret;
  8. @Value("${sendSMS.signName}")
  9. private String signName;
  10. @Value("${sendSMS.templateCode}")
  11. private String templateCode;
  12. @Value("${sendSMS.endpoint}")
  13. private String endpoint;
  14. public SendSmsResponse sendSMS(String phoneNumber, String code) throws Exception {
  15. com.aliyun.dysmsapi20170525.Client client = createClient(accessKeyId, accessKeySecret);
  16. HashMap<String, String> map = new HashMap<>();
  17. map.put("code", code);
  18. code = new JSONObject(map).toString();
  19. SendSmsRequest sendSmsRequest = new SendSmsRequest()
  20. .setPhoneNumbers(phoneNumber)
  21. .setSignName(signName)
  22. .setTemplateCode(templateCode)
  23. .setTemplateParam(code);
  24. return client.sendSms(sendSmsRequest);
  25. }
  26. public com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
  27. Config config = new Config()
  28. .setAccessKeyId(accessKeyId)
  29. .setAccessKeySecret(accessKeySecret);
  30. config.endpoint = endpoint; // 域名
  31. return new com.aliyun.dysmsapi20170525.Client(config);
  32. }
  33. }

二.发送邮箱MailUtil

  1. @Slf4j
  2. @Component
  3. public class MailUtil {
  4. @Value("${email.emailFrom}")
  5. String emailFrom;
  6. @Value("${email.host}")
  7. String host;
  8. @Value("${email.authorizationCode}")
  9. String authorizationCode;
  10. @Value("${email.emailContent}")
  11. String emailContent;
  12. @Value("${email.emailTo}")
  13. String emailTo;
  14. public void sendNoticeEmails(String description) throws Exception {
  15. // 1.创建连接对象javax.mail.Session
  16. // 2.创建邮件对象 javax.mail.Message
  17. // 3.发送一封激活邮件
  18. String from = emailFrom;// 发件人电子邮箱
  19. Properties properties = System.getProperties();// 获取系统属性
  20. properties.setProperty("mail.smtp.host", host);// 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)
  21. properties.setProperty("mail.smtp.auth", "true");// 打开认证
  22. //QQ邮箱需要下面这段代码,163邮箱不需要
  23. MailSSLSocketFactory sf = new MailSSLSocketFactory();
  24. sf.setTrustAllHosts(true);
  25. properties.put("mail.smtp.ssl.enable", "true");
  26. properties.put("mail.smtp.ssl.socketFactory", sf);
  27. // 1.获取默认session对象
  28. Session session = Session.getDefaultInstance(properties, new Authenticator() {
  29. public PasswordAuthentication getPasswordAuthentication() {
  30. return new PasswordAuthentication(emailFrom, authorizationCode); // 发件人邮箱账号、授权码
  31. }
  32. });
  33. // 2.创建邮件对象
  34. Message message = new MimeMessage(session);
  35. // 2.1设置发件人
  36. message.setFrom(new InternetAddress(from));
  37. // 2.2设置接收人
  38. message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));
  39. // 2.3设置邮件主题
  40. message.setSubject("系统异常通知");
  41. // 2.4设置邮件内容
  42. String content = "<html><head></head><body><h1>系统发生异常通知,请前往后台处理</h1><h3>通知信息为:" + description + "</h3></body></html>";
  43. message.setContent(content, "text/html;charset=UTF-8");
  44. // 3.发送邮件
  45. Transport.send(message);
  46. }
  47. }

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

闽ICP备14008679号