200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 对接阿里云sms短信服务发送验证码

对接阿里云sms短信服务发送验证码

时间:2019-11-24 18:33:31

相关推荐

对接阿里云sms短信服务发送验证码

1.购买阿里云短信服务

2.申请签名

3.申请短信模板

4.获取密钥

5.maven依赖

<dependency><groupId>com.aliyun</groupId><artifactId>dysmsapi0525</artifactId><version>2.0.9</version></dependency>

6.发送短信工具类

package com.thk.utils;import com.aliyun.dysmsapi0525.models.SendSmsRequest;import com.aliyun.teaopenapi.models.Config;import com.aliyun.teautil.models.RuntimeOptions;import com.thk.constant.Constant;import org.ponent;@Componentpublic class Sample {/*** 使用AK&SK初始化账号Client** @return Client* @throws Exception*/public static com.aliyun.dysmsapi0525.Client createClient() throws Exception {Config config = new Config()// 您的AccessKey ID.setAccessKeyId(Constant.ACCESSKEYID)// 您的AccessKey Secret.setAccessKeySecret(Constant.ACCESSKEYSECRET);// 访问的域名config.endpoint = "";return new com.aliyun.dysmsapi0525.Client(config);}public void sendSms(String code) {try {com.aliyun.dysmsapi0525.Client client = Sample.createClient();SendSmsRequest sendSmsRequest = new SendSmsRequest()//签名.setSignName("申请的签名")//模板.setTemplateCode("申请的模板").setPhoneNumbers("需要发送的手机号码")//验证码.setTemplateParam("{code:+" + code + "}");RuntimeOptions runtime = new RuntimeOptions();// 复制代码运行请自行打印 API 的返回值client.sendSmsWithOptions(sendSmsRequest, runtime);System.out.println("发送成功!!!");} catch (Exception e) {e.printStackTrace();System.out.println("发送失败!!!");}}}

7.测试

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。