当前位置:   article > 正文

SpringBoot-短信发送_springboot发送短信

springboot发送短信

1、环境准备

1 打开腾讯云短信服务,领取免费短信

国内短信活动_短信优惠活动_短信新购活动 - 腾讯云

2 打开腾讯公众号申请平台,申请公众号,申请类型选择个人类型。

记录下公平号名称,

微信公众平台

3 登录腾讯云短信发送平台,按照步骤申请国内短信发送

登录 - 腾讯云

4 申请短信签名和短信模板,等待审核通过,获取审核通过的短信模板ID和短信签名

5 获取腾讯密钥: secretId和secretKey

登录 - 腾讯云

6 获取个人短信SDK

登录 - 腾讯云

7 下载短信发送依赖

  1. <!--腾讯云短信-->
  2. <dependency>
  3. <groupId>com.tencentcloudapi</groupId>
  4. <artifactId>tencentcloud-sdk-java</artifactId>
  5. <version>3.1.424</version>
  6. </dependency>

2 短信发送

1 创建短信发送配置类,代码如下:

  1. package com.hqyj.config;
  2. import com.tencentcloudapi.common.Credential;
  3. import com.tencentcloudapi.common.profile.ClientProfile;
  4. import com.tencentcloudapi.common.profile.HttpProfile;
  5. import com.tencentcloudapi.sms.v20210111.SmsClient;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. @Configuration
  9. public class TencentSmsConfig {
  10. @Bean
  11. public SmsClient getSmsCilent(){
  12. //创建腾讯云短信发送凭证对象,填入secretId和secretKey
  13. Credential cred = new Credential("填入secretId", "填入secretKey");
  14. HttpProfile httpProfile = new HttpProfile();
  15. httpProfile.setEndpoint("sms.tencentcloudapi.com");
  16. ClientProfile clientProfile = new ClientProfile();
  17. clientProfile.setHttpProfile(httpProfile);
  18. //创建短信发送服务平台对象
  19. SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
  20. return client;
  21. }
  22. }

2 创建短信发送工具类

  1. package com.hqyj.utile;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.tencentcloudapi.common.exception.TencentCloudSDKException;
  5. import com.tencentcloudapi.sms.v20210111.SmsClient;
  6. import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
  7. import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Component;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. @Component
  14. public class SmsSend {
  15. @Autowired
  16. SmsClient smsClient;
  17. public HashMap<String,Object> send(String tel) throws TencentCloudSDKException {
  18. HashMap<String,Object> map = new HashMap<String,Object>();
  19. //创建短信发送请求对象
  20. SendSmsRequest req = new SendSmsRequest();
  21. //接收短信手机号
  22. String[] phone = {tel};
  23. req.setPhoneNumberSet(phone);
  24. //个人短信模板模板Id
  25. req.setTemplateId("短信模板模板Id");
  26. //个人短信发送SDK
  27. req.setSmsSdkAppId("短信发送SDK");
  28. //个人短信签名
  29. req.setSignName("短信签名");
  30. //短信验证码
  31. String[] p = {"2345"};
  32. req.setTemplateParamSet(p);
  33. //发送对象,并获取获取响应对象
  34. SendSmsResponse resp = smsClient.SendSms(req);
  35. //将响应结果转换成字符串格式
  36. String info = SendSmsResponse.toJsonString(resp);
  37. // 打印结果信息
  38. System.out.println(info);
  39. try {
  40. //将字符串格式转换成Map 接口对象,获取短信发送成功的提示
  41. ObjectMapper mapper = new ObjectMapper();
  42. Map<String,Object> m = mapper.readValue(info, Map.class);
  43. List<Map<String,Object>> list =( List<Map<String,Object>>) m.get("SendStatusSet");
  44. System.out.println(list.get(0).get("Code"));
  45. if(list.get(0).get("Code").equals("Ok")){
  46. System.out.println("发送成功");
  47. }
  48. } catch (JsonProcessingException e) {
  49. e.printStackTrace();
  50. }
  51. return map;
  52. }
  53. }

3 创建测试类,测试

  1. package com.hqyj.utile;
  2. import com.hqyj.MyApp;
  3. import com.tencentcloudapi.common.exception.TencentCloudSDKException;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. import static org.junit.Assert.*;
  10. @RunWith(SpringRunner.class)
  11. @SpringBootTest(classes = MyApp.class)
  12. public class SmsSendTest {
  13. @Autowired
  14. SmsSend smsSend;
  15. @Test
  16. public void send() {
  17. try {
  18. smsSend.send("电话");
  19. } catch (TencentCloudSDKException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }

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

闽ICP备14008679号