如何使用阿里云发送短信

需要开通阿里云 短信服务的模板和签名

//需要导入的jar

		<dependency>
			<groupId>com.aliyun</groupId>
			<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
			<version>1.1.0</version>
		</dependency>
		<dependency>
			<groupId>com.aliyun</groupId>
			<artifactId>aliyun-java-sdk-core</artifactId>
			<version>4.1.0</version>
		</dependency>

//自己封装的code类

package com.how2j.copy.vo;

public class Code {
    private String code;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

//官方测试使用的代码 稍作修改 即可

package com.how2j.copy.util;

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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.how2j.copy.vo.Code;

public class AliyunSmsUtils {
//这些都改为自己对应的
    private static String mySign = "***";
    private static String myTemplate = "*";
    private static String accessKey = "*****N";
    private static String accessSecret = "S*******g";




    public static boolean send(String code,String target){
        Code code1 = new Code();
        code1.setCode(code);

        String body = JSONObject.toJSONString(code1);
//        System.out.println(body);
//        System.out.println("{\"code\":\"4124\"}");
        /***************************/


        //区域 阿里云测试上并不是必须要填写的
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        //发送的目标
        request.putQueryParameter("PhoneNumbers", target);
        //签名名字,需要改成自己的
        request.putQueryParameter("SignName", mySign);
        //模板名字,需要改成自己的
        request.putQueryParameter("TemplateCode", myTemplate);
        //发送的验证码的code 需要遵守JSON格式

        request.putQueryParameter("TemplateParam", body);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (com.aliyuncs.exceptions.ServerException e) {
            e.printStackTrace();
            return false;
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }

        return true;
        /****************************/

    }


    public static void main(String[] args) {

        send("133455",139**66***01L+"");

/*
        //区域 阿里云测试上并不是必须要填写的
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAIH4zVEBDjvxXN", "SkjcM446fhobxCUWNvoVkeuXquOryg");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        //发送的目标
        request.putQueryParameter("PhoneNumbers", "139*******01");
        //签名名字,需要改成自己的
        request.putQueryParameter("SignName", "1*******");
        //模板名字,需要改成自己的
        request.putQueryParameter("TemplateCode", "SMS_16*****85");
        //发送的验证码的code 需要遵守JSON格式

        request.putQueryParameter("TemplateParam", "{\"code\":\"4124\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (com.aliyuncs.exceptions.ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }*/
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值