java阿里云发短信

首先创建要给Maven工程,因为需要引入阿里云为短信服务提供的sdk,
pom.xml如下:

       
            com.aliyun
            aliyun-java-sdk-core
            3.7.0
        
        
            com.aliyun
            aliyun-java-sdk-ecs
            4.11.0
        

下面的可以不要,因为sdk依赖的java版本较高,所以,我引入了下面的

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                
            
        
    

接下来就是简单的写代码过程啦:

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;

public class SendSms {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("default", "", "");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.GET);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumbers", "<手机号>");
        request.putQueryParameter("SignName", "SignName");
        request.putQueryParameter("TemplateCode", "SMS_172170657");
        request.putQueryParameter("TemplateParam", "{\"code\":\"this0is0a0test\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

上面的accessKeyIdaccessSecretSignName都是要购买该服务获取的,TemplateCode是要申请的,其他的我们可以根据情况设置。
上面的代码是一个发短信的案例,
下面我们来看一个查询案例:

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;
/*
pom.xml

  com.aliyun
  aliyun-java-sdk-core
  4.0.3

*/
public class QuerySendDetails {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("default", "", "");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("QuerySendDetails");
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumber", <手机号>);
        request.putQueryParameter("SendDate", "20190809");
        request.putQueryParameter("PageSize", "10");
        request.putQueryParameter("CurrentPage", "1");
//        request.putQueryParameter("BizId", "555");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

代码非常相识的,他们的动作参数不同而已。

你可能感兴趣的:(java阿里云发短信)