短信平台对接

来源:互联网 发布:兔子软件安装器 编辑:程序博客网 时间:2024/06/10 09:16

阿里云上提供的DEMO比较混乱,这里的代码直接拿过去加入JAR就可以用

 

首先要购买阿里云的短信服务,获取必要的信息,然后使用以下代码

[java] view plain copy
  1. import com.aliyun.mns.client.CloudAccount;  
  2. import com.aliyun.mns.client.CloudTopic;  
  3. import com.aliyun.mns.client.MNSClient;  
  4. import com.aliyun.mns.common.ServiceException;  
  5. import com.aliyun.mns.model.BatchSmsAttributes;  
  6. import com.aliyun.mns.model.MessageAttributes;  
  7. import com.aliyun.mns.model.RawTopicMessage;  
  8. import com.aliyun.mns.model.TopicMessage;  
  9.   
  10. public class AlyMessage {  
  11.     public static void sendMessage(String phone,String message){  
  12.         /** 
  13.         *以下信息必填,否则可能出现推送成功,但无法收到短信的情况 
  14.         */  
  15.         String accessKeyId = "";        //密匙  
  16.         String accessKeySecret = "";   //密匙  
  17.         String endpoint = "https://41238.mns.cn-hangzhou.aliyuncs.com/";  //MNS域地址  
  18.         String topicName = "sms.topic-cn-hangzhou"//主题  
  19.         String model = "SMS_7612321303";   //短信模板  
  20.         String signName = "xx公司";   //短信签名  
  21.   
  22.         /** 
  23.          * Step 1. 获取主题引用 
  24.          */  
  25.         CloudAccount account = new CloudAccount(accessKeyId,accessKeySecret,endpoint);  
  26.         MNSClient client = account.getMNSClient();  
  27.         CloudTopic topic = client.getTopicRef(topicName);  
  28.   
  29.         /** 
  30.          * Step 2. 设置SMS消息体(必须) 
  31.          * 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。 
  32.          */  
  33.         RawTopicMessage msg = new RawTopicMessage();  
  34.         msg.setMessageBody("sms-message");  
  35.   
  36.         /** 
  37.          * Step 3. 生成SMS消息属性 
  38.          */  
  39.         MessageAttributes messageAttributes = new MessageAttributes();  
  40.         BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();  
  41.         // 3.1 设置发送短信的签名(SMSSignName)  
  42.         batchSmsAttributes.setFreeSignName(signName);  
  43.         // 3.2 设置发送短信使用的模板(SMSTempateCode)  
  44.         batchSmsAttributes.setTemplateCode(model);  
  45.         // 3.3 设置发送短信所使用的模板中参数对应的值(在短信模板中定义的,没有可以不用设置)  
  46.         BatchSmsAttributes.SmsReceiverParams smsReceiverParams = new BatchSmsAttributes.SmsReceiverParams();  
  47.         smsReceiverParams.setParam("code",message);  
  48.         // 3.4 增加接收短信的号码  
  49.         batchSmsAttributes.addSmsReceiver(phone, smsReceiverParams);  
  50.         messageAttributes.setBatchSmsAttributes(batchSmsAttributes);  
  51.         try {  
  52.             /** 
  53.              * Step 4. 发布SMS消息 
  54.              */  
  55.             TopicMessage ret = topic.publishMessage(msg, messageAttributes);  
  56.             System.out.println("MessageId: " + ret.getMessageId());  
  57.             System.out.println("MessageMD5: " + ret.getMessageBodyMD5());  
  58.         } catch (ServiceException se) {  
  59.             System.out.println(se.getErrorCode() + se.getRequestId());  
  60.             System.out.println(se.getMessage());  
  61.             se.printStackTrace();  
  62.         } catch (Exception e) {  
  63.             e.printStackTrace();  
  64.         }  
  65.         client.close();  
  66.     }  
  67.       
  68.     public static void main(String[] args) {  
  69.         sendMessage("11232137""123456");  
  70.     }  
  71.   
  72. }  

所需要的jar包,可在阿里云官网去下载

原创粉丝点击