短信发送工具类
Posted 二师弟&
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了短信发送工具类相关的知识,希望对你有一定的参考价值。
短信发送工具类
pom.xml添加依赖
<!--阿里云短信服务-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.16</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>2.1.0</version>
</dependency>
工具类
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
/**
* 短信发送工具类
*/
public class SMSUtils
/**
* 发送短信
* @param signName 签名
* @param templateCode 模板
* @param phoneNumbers 手机号
* @param param 参数
*/
public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param)
DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "", "");
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setSysRegionId("cn-beijing");
request.setPhoneNumbers(phoneNumbers);
request.setSignName(signName);
request.setTemplateCode(templateCode);
request.setTemplateParam("\\"code\\":\\""+param+"\\"");
try
SendSmsResponse response = client.getAcsResponse(request);
System.out.println("短信发送成功");
catch (ClientException e)
e.printStackTrace();
以上是关于短信发送工具类的主要内容,如果未能解决你的问题,请参考以下文章