Springboot achieve docking Tencent cloud messaging

1, rely on introducing pom

1      <dependency>
2             <groupId>com.github.qcloudsms</groupId>
3             <artifactId>qcloudsms</artifactId>
4             <version>1.0.6</version>
5         </dependency>

2, writing tools

 1 package com.chesupi.message.util;
 2 
 3 import com.github.qcloudsms.SmsSingleSender;
 4 import com.github.qcloudsms.SmsSingleSenderResult;
 5 import lombok.extern.slf4j.Slf4j;
 6 
 7 @Slf4j
 8 public class SmsUtil {
 9     private final static int appid = 1400XXXXXX;
10     private final static String appkey = "6def9c1d5314bce4070e4b4xxxxxxxxx";
11 
12     public static String sendMessage(int templateId, String smsSign, phoneNumber String, String [] replacedValues) {
 13 is  
14          String Status = "" ;
 15          // send an SMS to a mobile phone
 16          // 1. Tencent the AppID item their cloud
 17          @ 2. Tencent their cloud projects the AppKey
 18 is          // template ID templateId 3 messages
 19          // 4 signed name smsSign
 20 is          // .. 5 who sent phoneNumber
 21 is          // String phoneNumber = "17671775xxx";
 22 is          // 6. the codes: manually without the verification maximum six digits only code
 23 is          // String [] = {the params "876 032"};
 24          // 7. The send message objects 
25         SmsSingleSender ssender = new SmsSingleSender(appid,appkey);
26 
27         //地区,电话,模板ID,验证码,签名
28         try {
29             SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNumber, templateId, replacedValues, smsSign, "", "");
30             status=result.errMsg;
31             log.info("sms send status,template id [{}],phone is [{}],status is [{}] ",templateId,phoneNumber,status);
32         } catch (Exception e){
33             log.info("sms send status,template id [{}],phone is [{}],status is [{}] ", templateId, phoneNumber, Status, E);
 34 is          }
 35          return Status; // Status is sent successfully here only "the OK"
 36      }

3, calls the tools to send text messages

. 1  Package com.chesupi.message.service;
 2  
. 3  Import com.chesupi.message.util.SmsUtil;
 . 4  Import lombok.extern.slf4j.Slf4j;
 . 5  Import org.springframework.stereotype.Component;
 . 6  
. 7  @Component
 . 8  @ SLF4J
 . 9  public  class DealerRegisterMessageReceiver {
 10  
. 11      / ** 
12 is       * method of receiving a message
 13 is       * / 
14      public  void receiverMessage (String message) {
 15          the try {
 16              log.info ( "user registration receive a new message: [{}]", Message);
 . 17              String [] = messageArray message.split ( "," );
 18 is              String messageArray phoneNumber = [0 ];
 . 19              String [] = {replacedValues messageArray [. 1 ]};
 20 is              SmsUtil.sendMessage (888 878, "SMS signature name ", phoneNumber, replacedValues);   // 888 878 is templateId" message name signature "message signature replacedValues day Tencent this application is a parameter required message content 
21 is          } the catch (Exception E) {
 22 is              log.error (" Send SMS Exception, Message IS [{}] " , Message);
 23 is          }
 24      }
 25  
26 is  
27 }

carry out! 

 

The templateId smsSign on the other method parameters for SMS templates can be common this method!

 

Guess you like

Origin www.cnblogs.com/LKiss/p/11973543.html