Development steps
1. Open the SMS service to get the key secret
2. Configure the company name variable of the SMS template // for example, the initial login password or verification code
3. Code add dependency jar package // Maven implementation // can refer to the official demo, directly used
4. Test sending SMS // successfully
The source code
package com.wz.bpm.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.aliyuncs.CommonRequest; import com.aliyuncs.CommonResponse; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; @author gongzhihao ** / public class SmsUtil {private static Logger Logger = LoggerFactory.getLogger(SmsUtil.class); /** * * @param phone * @param password */ public static void sendSms(String phone,String password) { logger.info("phone=" + phone + ",password=" + password );
DefaultProfile profile = DefaultProfile.getProfile("default"."key"."secret"); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("RegionId"."default");
request.putQueryParameter("PhoneNumbers", phone);
request.putQueryParameter("SignName"."XXX");
request.putQueryParameter("TemplateCode"."XXX");
String var = "{\"code\":" + password + "}"; / / SMS code is in the template parameter field, fill out here is, the value of the specific request. PutQueryParameter ("TemplateParam", var);
try {
CommonResponse response = client.getCommonResponse(request);
logger.info(response.getData());
} catch (ServerException e) {
logger.error("Server exception :", e);
} catch (ClientException e) {
logger.error("Client exception :", e); }}}Copy the code
reference
The official documentation, https://help.aliyun.com/document_detail/112148.html?spm=a2c4g.11186623.6.646.74b460e2Cgp0k1
Official online demo api.aliyun.com/?spm=a2c4g…. }&tab=MOCK&lang=JAVA
Other articles yq.aliyun.com/articles/59…