無紡布廠家15838056980日常開發(fā)中,我們需要給用戶發(fā)送操作驗(yàn)證碼或者其他一些群發(fā)消息通知之類的短信。這時候一般都是使用大廠提供的短信接口,阿里云或者騰訊云等短信接口。今天我們來看看騰訊云短信接口的調(diào)用。 短信構(gòu)成:普通短信一般由2兩部分構(gòu)成,簽名和正文,正文里包含了變量 例如:[騰訊云]你的驗(yàn)證碼是:。“騰訊云”就是你申請的簽名,“你的驗(yàn)證碼是:”是正文.""是變量,個人認(rèn)證用戶創(chuàng)建的短信正文模板每個變量取值最多支持12個字,企業(yè)用戶沒有限制。變量可以是多個按照的順序,我們在發(fā)送的時候按照變量順序?qū)?shù)傳入就可以了。 1.注冊騰訊云(需要實(shí)名驗(yàn)證,個人可以注冊)并且注冊贈送100條 2.創(chuàng)建簽名(必須先創(chuàng)建簽名) 個人申請簽名的支持APP、網(wǎng)站、、小程序.需要提供后臺的截圖,如小程序的話需要提供小程序管理后臺的截圖如: 3.創(chuàng)建短信模板 這里我們使用:為您的登錄驗(yàn)證碼,請于分鐘內(nèi)填寫。如非本人操作,請忽略本短信。為模板。 4.構(gòu)建工具類 1.取得你賬號騰訊云的SecretId和SecretKey 點(diǎn)擊右上角賬戶圖標(biāo)然后選擇賬戶信息,選擇左側(cè)訪問管理,點(diǎn)擊訪問密鑰,API密鑰管理 2.取得應(yīng)用ID 3.工具類 添加依賴: 工具類: import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.common.exception.TencentCloudSDKException; import com.tencentcloudapi.sms.v.SmsClient; import com.tencentcloudapi.sms.v.models.*; public class TxSendSmsUtil { private static String SecretId= "你的SecretId"; private static String SecretKey = "你的SecretKey"; //接口地址 private static String smsapiurl = "sms.tencentcloudapi.com"; //短信模板ID private static String TemplateID = "你的模板ID"; //短信簽名 private static String Sign = "你的簽名"; // 短信應(yīng)用 SDK AppID,SDK AppID 以1400開頭 private static String SmsSdkAppid = "你的應(yīng)用ID"; /** * 短信發(fā)送工具類 * @param phoneNumber接收的手機(jī)號碼 * @param templateParam模板中變量參數(shù) * @return */ public static String txSendSms(String[] phoneNumber,String[] templateParam) { try{ Credential cred = new Credential(SecretId, SecretKey); HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint(smsapiurl); ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); SmsClient client = new SmsClient(cred, "", clientProfile); SendSmsRequest req = new SendSmsRequest(); String[] phoneNumberSet1 = phoneNumber; req.setPhoneNumberSet(phoneNumberSet1); req.setTemplateID(TemplateID); req.setSign(Sign); String[] templateParamSet1 = templateParam; req.setTemplateParamSet(templateParamSet1); req.setSmsSdkAppid(SmsSdkAppid); SendSmsResponse resp = client.SendSms(req); String res = SendSmsResponse.toJsonString(resp); //System.out.println(res); return res; //System.out.println(SendSmsResponse.toJsonString(resp)); } catch (TencentCloudSDKException e) { System.out.println(e.toString()); return e.toString(); } } } 5.調(diào)用 //短信模板為:為您的登錄驗(yàn)證碼,請于分鐘內(nèi)填寫。如非本人操作,請忽略本短信。 String[] templateParamSet1 = new String[]{ "",//對用模板;//對應(yīng)模板;//需要在號碼前加入”+86“,可在工具類中處理 TxSendSmsUtil.txSendSms(phoneNumberSet1,templateParamSet1);//調(diào)用返回是字符串 用戶接收的信息: 【你的簽名】為您的登錄驗(yàn)證碼,請于3分鐘內(nèi)填寫。如非本人操作,請忽略本短信。 至此基礎(chǔ)的發(fā)送功能已經(jīng)實(shí)現(xiàn),下次再寫容錯、重試和群發(fā)的處理。

添加微信好友, 獲取更多信息
復(fù)制微信號

