1. 程式人生 > >發短信utils

發短信utils

阿裏雲 product 發短信 exceptio new 變量替換 ltr setprop com

package cn.itcast.bos.utils; 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; import com.aliyuncs.profile.IClientProfile; public class SendMsgUtils {
static final String product = "Dysmsapi"; //產品域名,開發者無需替換 static final String domain = "dysmsapi.aliyuncs.com"; // TODO 此處需要替換成開發者自己的AK(在阿裏雲訪問控制臺尋找) static final String accessKeyId = "LTAIikyTzqO63JmM"; static final String accessKeySecret = "owbpjtiqmuT2kmGPjSAR36E2gp3IcN"; public static SendSmsResponse sendSms(String telephone,String checkcode) throws ClientException { //可自助調整超時時間 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient,暫不支持region化 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); //組裝請求對象-具體描述見控制臺-文檔部分內容 SendSmsRequest request = new SendSmsRequest(); //必填:待發送手機號 request.setPhoneNumbers(telephone); //必填:短信簽名-可在短信控制臺中找到 request.setSignName("孟慶龍使用短信驗證註冊"); //必填:短信模板-可在短信控制臺中找到 request.setTemplateCode("SMS_109380407"); //可選:模板中的變量替換JSON串,如模板內容為"親愛的${name},您的驗證碼為${code}"時,此處的值為 request.setTemplateParam("{\"numcode\":\""+checkcode+"\"}"); //可選:outId為提供給業務方擴展字段,最終在短信回執消息中將此值帶回給調用者 request.setOutId("yourOutId"); //hint 此處可能會拋出異常,註意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); return sendSmsResponse; }
public static void main(String[] args) throws Exception { SendMsgUtils.sendSms("15731012160", "123123"); } }

發短信utils