/**
* @Description 阿里雲短信發送工具類
* @Author 曰業而安
*/
public class SendSmsUtil {
//phone 手機號
//param 驗證碼 注意這裏傳的參數名要和模板中設置的相對應
public static void SendSms(String phone, String param){
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("dysmsapi.aliyuncs.com");
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", phone);
request.putQueryParameter("SignName", 簽名);
request.putQueryParameter("TemplateCode",模板code );
request.putQueryParameter("TemplateParam", param);
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}
需要的依賴有
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.3.3</version>
</dependency>
項目中沒有gson jar包的還需要引入gson的jar。可參考博客:https://blog.csdn.net/duan196_118/article/details/107094557
另外,短信購買後申請簽名和模板可參考博客:https://blog.csdn.net/qq_37356556/article/details/104780705
希望對看到的小夥伴有所幫助。望不吝賜教!