生活随笔
收集整理的這篇文章主要介紹了
短信平台对接
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
阿里云上提供的DEMO比較混亂,這里的代碼直接拿過去加入JAR就可以用
?
首先要購買阿里云的短信服務,獲取必要的信息,然后使用以下代碼
[java]?view plaincopy
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?AlyMessage?{?? ????public?static?void?sendMessage(String?phone,String?message){?? ????????? ? ?? ????????String?accessKeyId?=?"";?????????? ????????String?accessKeySecret?=?"";????? ????????String?endpoint?=?"https://41238.mns.cn-hangzhou.aliyuncs.com/";??//MNS域地址?? ????????String?topicName?=?"sms.topic-cn-hangzhou";??? ????????String?model?=?"SMS_7612321303";????? ????????String?signName?=?"xx公司";????? ?? ????????? ? ?? ????????CloudAccount?account?=?new?CloudAccount(accessKeyId,accessKeySecret,endpoint);?? ????????MNSClient?client?=?account.getMNSClient();?? ????????CloudTopic?topic?=?client.getTopicRef(topicName);?? ?? ????????? ? ? ?? ????????RawTopicMessage?msg?=?new?RawTopicMessage();?? ????????msg.setMessageBody("sms-message");?? ?? ????????? ? ?? ????????MessageAttributes?messageAttributes?=?new?MessageAttributes();?? ????????BatchSmsAttributes?batchSmsAttributes?=?new?BatchSmsAttributes();?? ?????????? ????????batchSmsAttributes.setFreeSignName(signName);?? ?????????? ????????batchSmsAttributes.setTemplateCode(model);?? ?????????? ????????BatchSmsAttributes.SmsReceiverParams?smsReceiverParams?=?new?BatchSmsAttributes.SmsReceiverParams();?? ????????smsReceiverParams.setParam("code",message);?? ?????????? ????????batchSmsAttributes.addSmsReceiver(phone,?smsReceiverParams);?? ????????messageAttributes.setBatchSmsAttributes(batchSmsAttributes);?? ????????try?{?? ????????????? ? ?? ????????????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();?? ????}?? ?????? ????public?static?void?main(String[]?args)?{?? ????????sendMessage("11232137",?"123456");?? ????}?? ?? }??
所需要的jar包,可在阿里云官網去下載
總結
以上是生活随笔為你收集整理的短信平台对接的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。