java实现对阿里云消息服务实现发送短信
2017-04-12 16:34
686 查看
第一次使用阿里云的短信服务,正赶上阿里云短信服务和消息服务整合费了不少事,也走了不少弯了 现在开始写点心得,阿里的api虽然易看但是感觉还是复杂点,相对于中国网建这种的短信服务商麻烦了不少.
首先开始注册一个阿里云账号
找到消息服务
开通消息服务之后进入控制台.找到消息服务
选择好区域后,创建短信模板和签名 ,创建的时候需要上传企业的证件
之后创建主题和订阅
两种方式去实现代码,一种是创建java project 项目 将下载的sdk里面的jsrbao导入并build path ,一种是创建一个maven项目直接在pom文件添加下面这段代码
<dependency>
<groupId>com.aliyun.mns</groupId>
<artifactId>aliyun-sdk-mns</artifactId>
<version>1.1.8</version>
<classifier>jar-with-dependencies</classifier>
</dependency>
然后直接就可以写demo了,注意填写参数位置不要写错就ok了
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 BatchPublishSMSMessageDemo {
public static void main(String[] args) {
/**
* Step 1. get topic reference
*/
CloudAccount account = new CloudAccount("此处填写你的AccessId",
"access密钥", "sms域名地址");
MNSClient client = account.getMNSClient();
CloudTopic topic = client.getTopicRef("主题");
/**
* Step 2. set SMS message body ( required )
*/
RawTopicMessage msg = new RawTopicMessage();
msg.setMessageBody("sms-message");
/**
* Step 3. generate SMS message attributes
*/
MessageAttributes messageAttributes = new MessageAttributes();
BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();
// 3.1 set SMS message sign name
batchSmsAttributes.setFreeSignName("签名");
// 3.2 set SMS message template code
batchSmsAttributes.setTemplateCode("模板code");
// 3.3 set SMS message receiver param (defined in SMS message template)
BatchSmsAttributes.SmsReceiverParams smsReceiverParams = new BatchSmsAttributes.SmsReceiverParams();
smsReceiverParams.setParam("模板里的${}", "111");
// smsReceiverParams.setParam("$YourSMSTemplateParamKey2", "$value2");
// 3.4 add phone number of receiver (200 receivers at most)
batchSmsAttributes.addSmsReceiver("接收短信的手机号", smsReceiverParams);
// batchSmsAttributes.addSmsReceiver("$YourReceiverPhoneNumber2", smsReceiverParams);
messageAttributes.setBatchSmsAttributes(batchSmsAttributes);
try {
/**
* Step 4. publish SMS message
*/
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();
}
}
域名可以在这里获取
首先开始注册一个阿里云账号
找到消息服务
开通消息服务之后进入控制台.找到消息服务
选择好区域后,创建短信模板和签名 ,创建的时候需要上传企业的证件
之后创建主题和订阅
两种方式去实现代码,一种是创建java project 项目 将下载的sdk里面的jsrbao导入并build path ,一种是创建一个maven项目直接在pom文件添加下面这段代码
<dependency>
<groupId>com.aliyun.mns</groupId>
<artifactId>aliyun-sdk-mns</artifactId>
<version>1.1.8</version>
<classifier>jar-with-dependencies</classifier>
</dependency>
然后直接就可以写demo了,注意填写参数位置不要写错就ok了
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 BatchPublishSMSMessageDemo {
public static void main(String[] args) {
/**
* Step 1. get topic reference
*/
CloudAccount account = new CloudAccount("此处填写你的AccessId",
"access密钥", "sms域名地址");
MNSClient client = account.getMNSClient();
CloudTopic topic = client.getTopicRef("主题");
/**
* Step 2. set SMS message body ( required )
*/
RawTopicMessage msg = new RawTopicMessage();
msg.setMessageBody("sms-message");
/**
* Step 3. generate SMS message attributes
*/
MessageAttributes messageAttributes = new MessageAttributes();
BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();
// 3.1 set SMS message sign name
batchSmsAttributes.setFreeSignName("签名");
// 3.2 set SMS message template code
batchSmsAttributes.setTemplateCode("模板code");
// 3.3 set SMS message receiver param (defined in SMS message template)
BatchSmsAttributes.SmsReceiverParams smsReceiverParams = new BatchSmsAttributes.SmsReceiverParams();
smsReceiverParams.setParam("模板里的${}", "111");
// smsReceiverParams.setParam("$YourSMSTemplateParamKey2", "$value2");
// 3.4 add phone number of receiver (200 receivers at most)
batchSmsAttributes.addSmsReceiver("接收短信的手机号", smsReceiverParams);
// batchSmsAttributes.addSmsReceiver("$YourReceiverPhoneNumber2", smsReceiverParams);
messageAttributes.setBatchSmsAttributes(batchSmsAttributes);
try {
/**
* Step 4. publish SMS message
*/
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();
}
}
域名可以在这里获取
相关文章推荐
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- 调用阿里云接口实现短信消息的发送源码——CSDN博客
- php 阿里云短信服务及阿里大于实现短信验证码的发送
- 使用阿里云消息服务mns发送短信
- java实现短信消息发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- php 阿里云短信服务及阿里大鱼实现短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Java阿里云通信短信服务发送短信调用
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送