日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

短信平台对接

發布時間:2025/3/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 短信平台对接 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

阿里云上提供的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公司";???//短信簽名??
  • ??
  • ????????/**?
  • ?????????*?Step?1.?獲取主題引用?
  • ?????????*/??
  • ????????CloudAccount?account?=?new?CloudAccount(accessKeyId,accessKeySecret,endpoint);??
  • ????????MNSClient?client?=?account.getMNSClient();??
  • ????????CloudTopic?topic?=?client.getTopicRef(topicName);??
  • ??
  • ????????/**?
  • ?????????*?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(model);??
  • ????????//?3.3?設置發送短信所使用的模板中參數對應的值(在短信模板中定義的,沒有可以不用設置)??
  • ????????BatchSmsAttributes.SmsReceiverParams?smsReceiverParams?=?new?BatchSmsAttributes.SmsReceiverParams();??
  • ????????smsReceiverParams.setParam("code",message);??
  • ????????//?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();??
  • ????}??
  • ??????
  • ????public?static?void?main(String[]?args)?{??
  • ????????sendMessage("11232137",?"123456");??
  • ????}??
  • ??
  • }??

  • 所需要的jar包,可在阿里云官網去下載

    總結

    以上是生活随笔為你收集整理的短信平台对接的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。