阿里云短信服务_发送手机验证码java
来源:互联网 发布:见过最漂亮的女生知乎 编辑:程序博客网 时间:2024/05/10 23:07
1、首先你需要有阿里云账号---然后开通短信服务,记得在阿里云账号要有余额,不然发送不成功
---------------------------------------------------------------
--------------------------------------------------------------
-------------------------价格----------------------------------
-----------------------------------------------------------------
2、首先你需要准备的参数
public static String regionId = "cn-beijing";//机房信息,可以不用更改
/**********需的参数**************/
private final static String accessKey = "";
private final static String accessSecret = "";
private final static String code = "SMS_63270329";
private final static String signName = "葡萄科技";
创建和查看Access Key
https://ak-console.aliyun.com/#/accesskey/
3、开通短信服务成功后,需要配置短信签名和短信模板,配置完后,需要审核,审核过后就拿了code和signName两个参数
4、创建短信签名
5、创建短信模板
6、短信签名和短信模板审核通过后就可以开发了
7、主要代码
package coin.aliyun;
import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.CloudTopic;
import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.common.ServiceException;
import com.aliyun.mns.model.BatchSmsAttributes;
import com.aliyun.mns.model.MessageAttributes;
import com.aliyun.mns.model.RawTopicMessage;
import com.aliyun.mns.model.TopicMessage;
public class BatchPublishSMSMessage{
/**********需要准备的参数**************/
public static String regionId = "cn-beijing";//机房信息,可以不用更改
/**********需的参数**************/
private final static String accessKey = ""; 自己的accessKeyId
private final static String accessSecret = ""; 自己的accessSecret
private final static String code = "SMS_63270329"; 签名模板
private final static String signName = "葡萄科技";
/**********************************/
public static void main(String[] args) {
String phone="13716248260";
//根据自己定义的短信模板,修改
String jsonStr="8222";
SingleSendSms(phone, jsonStr);
}
public static void SingleSendSms (String phone, String jsonStr){
/**
* Step 1. 获取主题引用
*/
CloudAccount account = new CloudAccount(accessKey, accessSecret, "http://1145534441844957.mns.cn-beijing.aliyuncs.com");
MNSClient client = account.getMNSClient();
CloudTopic topic = client.getTopicRef("sms.topic-cn-beijing"); sms.topic-cn-beijing主题名称
/**
* Step 2. 设置SMS消息体(必须)
*
* 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。
*/
RawTopicMessage msg = new RawTopicMessage();
msg.setMessageBody("sms-message");
/**
* Step 3. 生成SMS消息属性
*/
MessageAttributes messageAttributes = new MessageAttributes();
BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();
// 3.1 设置发送短信的签名(SMSSignName)
batchSmsAttributes.setFreeSignName(signName);
// 3.2 设置发送短信使用的模板(SMSTempateCode)
batchSmsAttributes.setTemplateCode(code);
// 3.3 设置发送短信所使用的模板中参数对应的值(在短信模板中定义的,没有可以不用设置)
BatchSmsAttributes.SmsReceiverParams smsReceiverParams = new BatchSmsAttributes.SmsReceiverParams();
smsReceiverParams.setParam("jsonStr",jsonStr);
// 3.4 增加接收短信的号码
batchSmsAttributes.addSmsReceiver(phone, smsReceiverParams);
messageAttributes.setBatchSmsAttributes(batchSmsAttributes);
try {
/**
* Step 4. 发布SMS消息
*/
TopicMessage ret = topic.publishMessage(msg,messageAttributes);
System.out.println("MessageId: " + ret.getMessageId());
System.out.println("MessageMD5: " + ret.getMessageBodyMD5());
} catch (ServiceException se) {
System.out.println(se.getErrorCode() + se.getRequestId());
System.out.println(se.getMessage());
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
client.close();
}
}
8、成功import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.CloudTopic;
import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.common.ServiceException;
import com.aliyun.mns.model.BatchSmsAttributes;
import com.aliyun.mns.model.MessageAttributes;
import com.aliyun.mns.model.RawTopicMessage;
import com.aliyun.mns.model.TopicMessage;
public class BatchPublishSMSMessage{
/**********需要准备的参数**************/
public static String regionId = "cn-beijing";//机房信息,可以不用更改
/**********需的参数**************/
private final static String accessKey = ""; 自己的accessKeyId
private final static String accessSecret = ""; 自己的accessSecret
private final static String code = "SMS_63270329"; 签名模板
private final static String signName = "葡萄科技";
/**********************************/
public static void main(String[] args) {
String phone="13716248260";
//根据自己定义的短信模板,修改
String jsonStr="8222";
SingleSendSms(phone, jsonStr);
}
public static void SingleSendSms (String phone, String jsonStr){
/**
* Step 1. 获取主题引用
*/
CloudAccount account = new CloudAccount(accessKey, accessSecret, "http://1145534441844957.mns.cn-beijing.aliyuncs.com");
MNSClient client = account.getMNSClient();
CloudTopic topic = client.getTopicRef("sms.topic-cn-beijing"); sms.topic-cn-beijing主题名称
/**
* Step 2. 设置SMS消息体(必须)
*
* 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。
*/
RawTopicMessage msg = new RawTopicMessage();
msg.setMessageBody("sms-message");
/**
* Step 3. 生成SMS消息属性
*/
MessageAttributes messageAttributes = new MessageAttributes();
BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();
// 3.1 设置发送短信的签名(SMSSignName)
batchSmsAttributes.setFreeSignName(signName);
// 3.2 设置发送短信使用的模板(SMSTempateCode)
batchSmsAttributes.setTemplateCode(code);
// 3.3 设置发送短信所使用的模板中参数对应的值(在短信模板中定义的,没有可以不用设置)
BatchSmsAttributes.SmsReceiverParams smsReceiverParams = new BatchSmsAttributes.SmsReceiverParams();
smsReceiverParams.setParam("jsonStr",jsonStr);
// 3.4 增加接收短信的号码
batchSmsAttributes.addSmsReceiver(phone, smsReceiverParams);
messageAttributes.setBatchSmsAttributes(batchSmsAttributes);
try {
/**
* Step 4. 发布SMS消息
*/
TopicMessage ret = topic.publishMessage(msg,messageAttributes);
System.out.println("MessageId: " + ret.getMessageId());
System.out.println("MessageMD5: " + ret.getMessageBodyMD5());
} catch (ServiceException se) {
System.out.println(se.getErrorCode() + se.getRequestId());
System.out.println(se.getMessage());
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
client.close();
}
}
0 0
- 阿里云短信服务_发送手机验证码java
- 使用阿里云的短信服务发送短信验证码
- 发送短信验证码到手机(阿里大于平台) java
- 阿里云-----云通信短信服务---发送验证码到手机
- ThinkPHP5 集成阿里云SMS短信服务--验证码发送
- Thinkphp5+阿里云SMS短信服务发送验证码
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- 阿里云发送短信验证码
- 阿里云短信验证码发送类
- 阿里云发送短信验证码
- 阿里云--发送短信或短信验证码
- 阿里云PHP-SMS短信服务——验证码发送教程
- 阿里云PHP-SMS短信服务——验证码发送教程
- 利用阿里大于发送短信验证码(java)
- java实现对阿里云消息服务实现发送短信
- 关于调用阿里大鱼发送手机验证码短信同一账号发送多次后失败
- 阿里云短信验证码发送工具类
- 使用阿里大于发送短信验证码
- php实现邮件发送功能.
- MTK 软件设置路径(铃声、开关机动画、壁纸等)
- ffmpeg源码简析(十二)FFMPEG中的主要结构体总结
- ecshop后台的新建分页
- 常用二叉树总结
- 阿里云短信服务_发送手机验证码java
- JRE和JDK的区别
- 第八章 React组件的生命周期
- HDU5242:Game(树上贪心)
- Mac 终端使用VPN (已解决)
- WOW.js+animate 帮助你实现滚动页面动画效果
- Qt学习整理
- wireshark学习笔记(一)
- 重载函数为什么只在C++下