使用前提与环境准备:
操作步骤:
1.添加pom依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.23</version>
</dependency>
2.Code Sample
// This file is auto-generated, don't edit it. Thanks.
import com.aliyun.dysmsapi20170525.models.SendBatchSmsResponse;
import com.aliyun.tea.*;
import com.aliyun.teautil.models.RuntimeOptions;
import com.google.gson.Gson;
//批量发送短发快速调用
public class Sample {
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,您的 AccessKey ID
.setAccessKeyId(accessKeyId)
// 必填,您的 AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
// 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html
com.aliyun.dysmsapi20170525.Client client = Sample.createClient("XXXXXXXXXX", "XXXXXXXXXX");
com.aliyun.dysmsapi20170525.models.SendBatchSmsRequest sendBatchSmsRequest = new com.aliyun.dysmsapi20170525.models.SendBatchSmsRequest()
.setPhoneNumberJson("[\"13523484707\",\"18790673693\"]")
.setSignNameJson("[\"阿里云计算有限公司\",\"阿里云计算有限公司\"]")
.setTemplateCode("SMS_268350087")
.setTemplateParamJson("[{\"code\":\"123456\"},{,\"code\":\"34567\"}]");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
RuntimeOptions runtimeOptions = new RuntimeOptions();
SendBatchSmsResponse sendBatchSmsResponse1 = client.sendBatchSmsWithOptions(sendBatchSmsRequest, runtimeOptions);
try {
// 复制代码运行请自行打印 API 的返回值
SendBatchSmsResponse sendBatchSmsResponse = client.sendBatchSmsWithOptions(sendBatchSmsRequest, runtime);
System.out.println(new Gson().toJson(sendBatchSmsResponse.getBody()));
System.out.println(new Gson().toJson(sendBatchSmsResponse1.getBody()));
} catch (TeaException error) {
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
System.out.println(new Gson().toJson(sendBatchSmsResponse1));
}
}
}
3.测试结果
{“bizId":"492400578276963230^0","code":"OK","message":"OK","requestId":"B077AED4-D18D-5596-B4B0-012EA43E472C"}
{"bizId":"584908178276963481^0","code":"OK","message":"OK","requestId":"7D75D27C-2619-5F03-AD54-BD13C01B9255"}
更多参考
短信服务:快速入门
短信服务:API参考