阿里云发送短信

  1. Sdk
 		<dependency>
            <groupId>com.aliyungroupId>
            <artifactId>aliyun-java-sdk-coreartifactId>
            <version>4.5.16version>
        dependency>

        <dependency>
            <groupId>com.aliyungroupId>
            <artifactId>aliyun-java-sdk-dysmsapiartifactId>
            <version>2.2.1version>
        dependency>

  1. 发送参数
    阿里云发送短信_第1张图片

  2. 后端代码

package com.atguigu.gulimallthirdparty.component;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class SendSms {

    @Value("${spring.cloud.alicloud.sms.access-key-id}")
    private String accessId;

    @Value("${spring.cloud.alicloud.sms.secret}")
    private String secretI;

    @Value("${spring.cloud.alicloud.sms.sign-name}")
    private String SignName;

    @Value("${spring.cloud.alicloud.sms.template-code}")
    private String templateCode;


    public boolean send(String phoneNum, String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", accessId, secretI);
        IAcsClient client = new DefaultAcsClient(profile);
        //构建请求
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        //自定义参数(手机号,验证码,签名,模板)
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", SignName);
        request.putQueryParameter("TemplateCode", templateCode);
        //验证码 {"code":"1234"}
        request.putQueryParameter("TemplateParam","{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);
            return false;
        }

    }

}

你可能感兴趣的:(阿里云,spring,java)