Java后端阿里云短信平台发送短信
生活随笔
收集整理的這篇文章主要介紹了
Java后端阿里云短信平台发送短信
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近做了關于阿里云平臺發送短信的功能,記錄下代碼方便以后查閱:
@Service public class ALiYunSMSServiceImpl implements ALiYunSMSService {@Autowiredprivate AliyunSMSProperties aliyunSMSProperties;/*** 短信發送驗證碼* @param aliyunSMSInfo* @return*/@Overridepublic AliyunSMSReturnInfo sendSMSToVertifyCode(AliyunSMSInfo aliyunSMSInfo) {AliyunSMSReturnInfo aliyunSMSReturnInfo = new AliyunSMSReturnInfo();aliyunSMSReturnInfo.setReturnCode(AliyunSMSConstants.RETURN_FAIL);// 默認返回失敗/*** Step 1. 獲取主題引用*/CloudAccount account = new CloudAccount(aliyunSMSProperties.getAccessId(), aliyunSMSProperties.getAccessKey(),aliyunSMSProperties.getMnsEndpoint());MNSClient client = account.getMNSClient();CloudTopic topic = client.getTopicRef(aliyunSMSProperties.getTopic());/*** Step 2. 設置SMS消息體(必須)** 注:目前暫時不支持消息內容為空,需要指定消息內容,不為空即可。*/RawTopicMessage msg = new RawTopicMessage();msg.setMessageBody(AliyunSMSConstants.MESSAGEBODY_SMS_MESSAGE);/*** Step 3. 生成SMS消息屬性*/MessageAttributes messageAttributes = new MessageAttributes();BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();// 3.1 設置發送短信的簽名(SMSSignName)batchSmsAttributes.setFreeSignName(aliyunSMSProperties.getSignName());// 3.2 設置發送短信使用的模板(SMSTempateCode)batchSmsAttributes.setTemplateCode(aliyunSMSInfo.getSmsTemplateCode());// 3.3 設置接收短信的手機號碼(在短信模板中定義的)BatchSmsAttributes.SmsReceiverParams smsReceiverParams = new BatchSmsAttributes.SmsReceiverParams();smsReceiverParams.setParam(AliyunSMSConstants.REGISTER_VERTIFY_TEMPLATE_CODE,aliyunSMSInfo.getSmsVertifyCode());// 模板里面{}部分smsReceiverParams.setParam(AliyunSMSConstants.REGISTER_VERTIFY_TEMPLATE_PRODUCT,AliyunSMSConstants.REGISTER_VERTIFY_TEMPLATE_PRODUCT_NAME);// 模板里面{}部分// 3.4 增加接收短信的號碼batchSmsAttributes.addSmsReceiver(aliyunSMSInfo.getMobile(), smsReceiverParams);// batchSmsAttributes.addSmsReceiver("$YourReceiverPhoneNumber2",// smsReceiverParams);messageAttributes.setBatchSmsAttributes(batchSmsAttributes);try {/*** Step 4. 發布SMS消息*/TopicMessage ret = topic.publishMessage(msg, messageAttributes);aliyunSMSReturnInfo.setReturnCode(AliyunSMSConstants.RETURN_SUCCESS);// 返回成功aliyunSMSReturnInfo.setReturnMsg("MessageId: " + ret.getMessageId() + " MessageMD5: " + ret.getMessageBodyMD5());} catch (ServiceException se) {aliyunSMSReturnInfo.setReturnCode(AliyunSMSConstants.RETURN_FAIL);// 返回失敗aliyunSMSReturnInfo.setReturnMsg("ErrorCode: " + se.getErrorCode() + " RequestId: " + se.getRequestId()+ " Message:" + se.getMessage() + " Exception:" + se.toString());} catch (Exception e) {aliyunSMSReturnInfo.setReturnCode(AliyunSMSConstants.RETURN_FAIL);// 返回失敗aliyunSMSReturnInfo.setReturnMsg("Exception:" + e.toString());} finally {client.close();}return aliyunSMSReturnInfo;} //發送特定消息@Overridepublic AliyunSMSReturnInfo sendSMSMsg(List<String> mobileList, SmsReceiverParams smsReceiverParams,String smsTemplateCode) {AliyunSMSReturnInfo aliyunSMSReturnInfo = new AliyunSMSReturnInfo();aliyunSMSReturnInfo.setReturnCode(AliyunSMSConstants.RETURN_FAIL);// 默認返回失敗/*** Step 1. 獲取主題引用*/CloudAccount account = new CloudAccount(aliyunSMSProperties.getAccessId(), aliyunSMSProperties.getAccessKey(),aliyunSMSProperties.getMnsEndpoint());MNSClient client = account.getMNSClient();CloudTopic topic = client.getTopicRef(aliyunSMSProperties.getTopic());/*** Step 2. 設置SMS消息體(必須)** 注:目前暫時不支持消息內容為空,需要指定消息內容,不為空即可。*/RawTopicMessage msg = new RawTopicMessage();msg.setMessageBody(AliyunSMSConstants.MESSAGEBODY_SMS_MESSAGE);/*** Step 3. 生成SMS消息屬性*/MessageAttributes messageAttributes = new MessageAttributes();BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();// 3.1 設置發送短信的簽名(SMSSignName)batchSmsAttributes.setFreeSignName(aliyunSMSProperties.getSignName());// 3.2 設置發送短信使用的模板(SMSTempateCode)batchSmsAttributes.setTemplateCode(smsTemplateCode);// 3.3 設置發送短信所使用的模板中參數對應的值(在短信模板中定義的,沒有可以不用設置)// ...smsReceiverParams// 3.4 增加接收短信的號碼for (String mobile : mobileList) {batchSmsAttributes.addSmsReceiver(mobile, smsReceiverParams);}messageAttributes.setBatchSmsAttributes(batchSmsAttributes);try {/*** Step 4. 發布SMS消息*/TopicMessage ret = topic.publishMessage(msg, messageAttributes);aliyunSMSReturnInfo.setReturnCode(AliyunSMSConstants.RETURN_SUCCESS);// 返回成功aliyunSMSReturnInfo.setReturnMsg("MessageId: " + ret.getMessageId() + " MessageMD5: " + ret.getMessageBodyMD5());} catch (ServiceException se) {aliyunSMSReturnInfo.setReturnCode(AliyunSMSConstants.RETURN_FAIL);// 返回失敗aliyunSMSReturnInfo.setReturnMsg("ErrorCode: " + se.getErrorCode() + " RequestId: " + se.getRequestId()+ " Message:" + se.getMessage() + " Exception:" + se.toString());} catch (Exception e) {aliyunSMSReturnInfo.setReturnCode(AliyunSMSConstants.RETURN_FAIL);// 返回失敗aliyunSMSReturnInfo.setReturnMsg("Exception:" + e.toString());} finally {client.close();}return aliyunSMSReturnInfo;}} //返回消息 public class AliyunSMSReturnInfo {private String returnCode;//返回碼 0-失敗 1-成功private String returnMsg;//返回信息public String getReturnCode() {return returnCode;}public void setReturnCode(String returnCode) {this.returnCode = returnCode;}public String getReturnMsg() {return returnMsg;}public void setReturnMsg(String returnMsg) {this.returnMsg = returnMsg;} /*** 阿里云短信發送驗證碼信息體* @author Administrator**/ public class AliyunSMSInfo {private String smsTemplateCode;// 短信模板CODEprivate String smsVertifyCode;// 驗證碼private String mobile;// 手機號public String getMobile() {return mobile;}public void setMobile(String mobile) {this.mobile = mobile;}public String getSmsVertifyCode() {return smsVertifyCode;}public void setSmsVertifyCode(String smsVertifyCode) {this.smsVertifyCode = smsVertifyCode;}public String getSmsTemplateCode() {return smsTemplateCode;}public void setSmsTemplateCode(String smsTemplateCode) {this.smsTemplateCode = smsTemplateCode;}} //阿里云短信配置信息 @Configuration @PropertySource("classpath:aliyunsms.properties") public class AliyunSMSProperties {@Value("${AccessId}")private String accessId;//@Value("${AccessKey}")private String accessKey;//@Value("${MNSEndpoint}")private String mnsEndpoint;//@Value("${Topic}")private String topic;// 主題名稱@Value("${SignName}")private String signName;// 短信簽名--注意是中文名稱public String getAccessId() {return accessId;}public void setAccessId(String accessId) {this.accessId = accessId;}public String getAccessKey() {return accessKey;}public void setAccessKey(String accessKey) {this.accessKey = accessKey;}public String getMnsEndpoint() {return mnsEndpoint;}public void setMnsEndpoint(String mnsEndpoint) {this.mnsEndpoint = mnsEndpoint;}public String getTopic() {return topic;}public void setTopic(String topic) {this.topic = topic;}public String getSignName() {return signName;}public void setSignName(String signName) {this.signName = signName;}} /*** 阿里云短信服務通用常量*/ public class AliyunSMSConstants {/*** SMS消息體 sms-message*/public static final String MESSAGEBODY_SMS_MESSAGE = "sms-message";/*** 阿里云-用戶注冊驗證碼模板${code} ${product}*/public static final String REGISTER_VERTIFY_TEMPLATE_CODE = "code";/*** 阿里云-用戶注冊驗證碼模板${code} ${product}*/public static final String REGISTER_VERTIFY_TEMPLATE_PRODUCT = "product";/*** 阿里云-用戶注冊驗證碼模板編號*/public static final String REGISTER_SMS_TEMPLATE_CODE = "REGISTER_SMS_TEMPLATE_CODE";/*** 阿里云-用戶忘記密碼模板編號*/public static final String FORGET_SMS_TEMPLATE_CODE = "FORGET_SMS_TEMPLATE_CODE ";/*** 通知用戶初始密碼*/public static final String SEND_PASSWORD_SMS_TEMPLATE_CODE = "SEND_PASSWORD_SMS_TEMPLATE_CODE ";/*** 預定發送短信*/public static final String RESERVATION_SMS_TEMPLATE_CODE = "RESERVATION_SMS_TEMPLATE_CODE ";/*** 阿里云-用戶注冊驗證碼模板${product} 名稱 */public static final String REGISTER_VERTIFY_TEMPLATE_PRODUCT_NAME = "編號";/*** 返回碼1-成功*/public static final String RETURN_SUCCESS = "1";/*** 返回碼0-失敗*/public static final String RETURN_FAIL = "0"; }調用代碼
/*** 通知用戶初始密碼 發送特定消息* @param phone 手機號* @param password 密碼* @param userName 用戶名*/private void sendNewPassWord(String phone, String password, String userName) {List<String> mobileList = new ArrayList<String>();mobileList.add(phone);//加入需要發送的手機號碼// mobileList.add("180000000000");SmsReceiverParams smsReceiverParams = new SmsReceiverParams();smsReceiverParams.setParam("name", userName);smsReceiverParams.setParam("psw", password);aliYunSMSService.sendSMSMsg(mobileList, smsReceiverParams, AliyunSMSConstants.SEND_PASSWORD_SMS_TEMPLATE_CODE);} //驗證碼調用代碼String verifyCode = String.valueOf(RandNumberUtil.getRandNum(1, 999999));// 發送短信AliyunSMSInfo aliyunSMSInfo = new AliyunSMSInfo();aliyunSMSInfo.setMobile(mobile);if (StringUtils.equals(type, "register")) {aliyunSMSInfo.setSmsTemplateCode(AliyunSMSConstants.REGISTER_SMS_TEMPLATE_CODE);} else if (StringUtils.equals(type, "forget")) {aliyunSMSInfo.setSmsTemplateCode(AliyunSMSConstants.FORGET_SMS_TEMPLATE_CODE);}aliyunSMSInfo.setSmsVertifyCode(verifyCode);AliyunSMSReturnInfo aliyunSMSReturnInfo = aliyunSMSService.sendSMSToVertifyCode(aliyunSMSInfo);總結
以上是生活随笔為你收集整理的Java后端阿里云短信平台发送短信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OTB数据集中的标签含义及对应的视频序列
- 下一篇: SkyIAR(简单高效的IDEAHCIR