java阿里云发短信

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

       <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>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

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

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", "<accessKeyId>", "<accessSecret>");
        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
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.0.3</version>
</dependency>
*/
public class QuerySendDetails {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("default", "<accessKeyId>", "<accessSecret>");
        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();
        }
    }
}

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 阿里云短信服务还是非常好的,接口稳定,同时还提供SDK,使得企业的接入该服务更加方便。下面来看看阿里云提供的发短信...
    small瓜瓜阅读 686评论 0 1
  • 阿里大鱼短信配置非常简单,只需引入阿里大鱼完整SDK,然后在自己的项目里面引用SDK目录autoload.php文...
    480a895cc5bf阅读 1,474评论 0 2
  • 公司要建立一个内部短信平台,需要发短信的业务,对比了三大运营商: 电信:没有此业务。 联通:需要短信网关(硬件),...
    Guncorpse阅读 9,067评论 0 4
  • 一、申请阿里的账号,并登陆 二、点击进入控制台 点击使用短信服务 进入短信服务概览页面 点击短信服务下方的java...
    亭台雨榭111阅读 6,523评论 2 14
  • 主语: 《诗·大雅·灵台》:“虡业维樅,賁鼓维鏞。”《乐府诗集·燕射歌辞三·食举歌八》:“桓蒲在位簨业张,加籩折俎...
    Peregrination阅读 175评论 0 0