阿里短信单发,批量发送_如何用阿里小号发短信
2023-06-13 09:15:11 时间
1.导入
<!--阿里云短信-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.3</version>
</dependency>
单条发送
/**
* 阿里云短信发送
*/
public String sendALMSM(String content, String mobile,String temNo) {
DefaultProfile profile = DefaultProfile.getProfile(PropertiesUtils.getInstance().getProperty("regionId"), PropertiesUtils.getInstance().getProperty("accessKeyId"), PropertiesUtils.getInstance().getProperty("secret"));
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("SendSms");
request.putQueryParameter("RegionId", PropertiesUtils.getInstance().getProperty("regionId"));
request.putQueryParameter("PhoneNumbers", mobile); //接收短信的手机号码。
request.putQueryParameter("SignName", PropertiesUtils.getInstance().getProperty("signName"));//短信签名名称
request.putQueryParameter("TemplateCode", temNo);//短信模板ID
request.putQueryParameter("TemplateParam", content);//短信模板变量对应的实际值,JSON格式 {"code":"1111"}
CommonResponse response= null;
try {
response = client.getCommonResponse(request);
System.out.println("短信接口返回的数据----------------");
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return response.getData();
}
批量发送
public static String sendALMSMPl(String mobile,String SignNameJson,String temNo,String content) {
DefaultProfileprofile = DefaultProfile.getProfile("cn-hangzhou", "", "");
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("SendBatchSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumberJson", mobile);
request.putQueryParameter("SignNameJson", SignNameJson);
request.putQueryParameter("TemplateCode", temNo);
request.putQueryParameter("TemplateParamJson", content);
CommonResponse response= null;
try {
response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return response.getData();
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/226759.html原文链接:https://javaforall.cn