欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

阿里云短信服务

程序员文章站 2024-02-29 18:44:34
...

今天学习下了阿里云短信服务,上代码
第一步 maven 依赖导入jar包

  <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>3.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-ecs</artifactId>
            <version>4.11.0</version>
        </dependency>

第二步

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;
public class test1 {
    //阿里大鱼
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "AccessKey", " Secret");  //登陆自己的阿里账号查看
        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", "你要发送的号码");
        request.putQueryParameter("SignName", "模板");   //这里用的模板,需要去设置更换模板
        request.putQueryParameter("TemplateCode", "SMS_160861668");
        request.putQueryParameter("TemplateParam", "{name:\"Tom\",code:\"456\"}");  //返回的json格式
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

成功·是这样的
{“Message”:“OK”,“RequestId”:“28C75FF8-8156-4FD0-BF5A-D1918F37C7EF”,“BizId”:“960919759222715779^0”,“Code”:“OK”}
失败的话会报原因,请查看开发者文档
这是地址:https://help.aliyun.com/document_detail/101346.html?spm=a2c4g.11186623.6.613.327b2246nUx7tw