2019-06-17 阿里云短信demo

1、第一步毋庸置疑的在阿里云控制台,短信服务,申请签名、短信模板,然后创建AccessKey  点击官网

2、点击进入阿里云短信发送官方API 

package com.alisms.test.sms;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @title  阿里云短信测试
 * @author yaco_fyk@163.com
 * @date   2019年6月17日 上午9:41:39
 */

/**
<!-- 阿里云短信sdk -->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.1.0</version>
</dependency> 
 */
public class TestAliSms {
	
	private static final String accessKey = "LTAIu**********RZ";
	private static final String secret = "FNw0**********t2G**********";
	
	public static void main(String[] args) {
		
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKey, 
        		secret);
        // 初始化client对象
        IAcsClient client = new DefaultAcsClient(profile);
        
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        // 产品域名,开发者无需替换
        request.setDomain("dysmsapi.aliyuncs.com");
        // 系统参数  不可以自己改动时间 new SimpleDateFormat("yyyy-MM-dd").format(new Date()) 必须是2017-5-25
        request.setVersion("2017-05-25");
        // 系统规定参数
        request.setAction("SendSms");
        // 发送的电话号码
        request.putQueryParameter("PhoneNumbers", "130*******6");
        // 签名管理->签名名称
        request.putQueryParameter("SignName", "XXX");
        // 模版管理->模版CODE
        request.putQueryParameter("TemplateCode", "SMS_16*****255");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "8888");
        request.putQueryParameter("TemplateParam", jsonObject.toJSONString());

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            JSONObject json = JSONObject.parseObject(response.getData());
            if ("OK".equals(json.get("Code"))) {
            	System.out.println("发送成功...");
            } else {
            	System.out.println("发送失败,失败消息为" + json.get("Message"));
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
	

}

 

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cocosum

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值