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、成功
|