preface

When it comes to SMS interface, I believe that every program monkey will not be unfamiliar, it can help us to achieve SMS verification code, SMS notification and other functions, just recently to help friends to develop a set of member management system, which also integrated SMS function (SMS interface platform is Tencent cloud platform), Take this opportunity to share with you Java code such as call Tencent cloud platform SMS interface.

By the way, most of the current SMS interface platforms need businesses or companies to authenticate to use, and each platform’s SMS prices are not the same, if you just want to learn, I recommend Tencent cloud platform (personal authentication can be used; Complimentary test message after authentication; Purchase prices are relatively low; Interface documentation details).

Register Tencent cloud platform and apply for signature

Want to use Tencent cloud SMS interface, need to do some preparatory work ~ first is to register a Tencent cloud platform (Tencent cloud link:cloud.tencent.com/) account and enter the console, in the upper left corner of “cloud products” find “SMS”, enter the SMS console. Then there is need to certification account and apply for signatures and message template, my account is a personal account of authentication, when applying for signature type “signature” choose the “public” (if signature type choice, public, you need to go to a WeChat WeChat open platform for the public, and then to apply for SMS signature), the back step by step according to clew operation.Note that you must apply for an SMS template after the signature is approved. Otherwise, the application cannot be approved.The above is the preparatory work, nonsense not to say, directly on the code

Calling the SMS Interface

public static void sendMessage(String phoneNumber) {
        try{
            // To instantiate an authentication object, enter the Tencent cloud accounts secretId and secretKey, and pay attention to the confidentiality of the key pair
            / / key can be get to https://console.cloud.tencent.com/cam/capi
            Credential cred = new Credential(secretId, secretKey);
            // Instantiate an HTTP option, optionally skipped without special requirements
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // Instantiate a client option, optional, no special requirements can be skipped
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // Instantiate the client object to request the product. ClientProfile is optional
            SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
            // Instantiate a request object, one for each interface
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet = {phoneNumber};    // SMS receiver number
            req.setPhoneNumberSet(phoneNumberSet);
            req.setSmsSdkAppId("xxxxx");           //SdkAppId
            req.setSignName("xxxxx");              // SMS signature
            req.setTemplateId("xxxxx");            // ID of the SMS template
            // Set parameters
            String[] templateParamSet = {DateUtils.getDate(),phoneNumber};
            req.setTemplateParamSet(templateParamSet);

            // The returned resp is an instance of SendSmsResponse corresponding to the request object
            SendSmsResponse resp = client.SendSms(req);
            // Outputs a string in json format
          	System.out.println(SendSmsResponse.toJsonString(resp));
        } catch(TencentCloudSDKException e) { System.out.println(e.toString()); }}Copy the code

There are a few things to note when calling interfaces:

  1. Access console.cloud.tencent.com/cam/capi into “access management console,” in “access key” – > “API key management” obtain secretId and secretKey
  2. To obtain the SdkAppId, choose SMS Console > Application Management > Application List. You can see the SdkAppId corresponding to the application in the application list
  3. ==req.setSignName(“xxxxx”); The value is set to the content of the applied signature, not the signature ID==
  4. When setting parameters, the sequence of parameters should be the same as that of {1}, {2}… Correspondingly, if your Tencent cloud platform account is authenticated by a personal account, the parameter length is also limited (the parameter length cannot be greater than 12).

summary

My experience is limited, some places may not be particularly in place, if you think of any questions when reading, welcome to leave a message in the comments section, we will discuss one by one 🙇

Please take a thumbs up and a follow (✿◡‿◡) for this article ~ a crab (●’◡’●)

If there are mistakes in the article, welcome to comment correction; If you have a better, more unique understanding, you are welcome to leave your valuable ideas in the comments area.

Love what you love, do what you do, listen to your heart and ask nothing