In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to send SMS with Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to send text messages with Java".
1. Introduce related maven dependency com.github.qcloudsms qcloudsms 1.0.6 com.qcloud qcloud-java-sdk 2.0.1 com.tencentcloudapi tencentcloud-sdk-java 3.1.270 cn.hutool hutool-all 5.4.22. Write a short message sending tool class package com.cloud.system.common.utils; import ch.qos.logback.core.net.server.Client;import cn.hutool.core.util.RandomUtil;import com.cloud.system.api.enums.SmsLengthEnum;import com.cloud.system.api.enums.SmsTypeEnum;import com.tencentcloudapi.common.Credential;import com.tencentcloudapi.common.exception.TencentCloudSDKException;import com.tencentcloudapi.common.profile.ClientProfile;import com.tencentcloudapi.common.profile.HttpProfile;import com.tencentcloudapi.sms.v20210111.SmsClient Import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;import com.tencentcloudapi.sms.v20210111.models.SendStatus;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Component; import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Properties / * @ program: server-master * @ description: * @ author: hjq * @ create: 2021-12-17 * / @ Component@Slf4jpublic class SmsUtil {/ * * Tencent Cloud account key pair secretId (configured in access management) * / private static String secretId; / * * Tencent Cloud account key pair secretKey (configured in access management) * / private static String secretKey / * * SMS Application appId * / private static String appId; / * * SMS App appKey * / private static String appKey; / * * signature * / private static String smsSign; / * * Expiration time * / private static String expireTime; / * * Login Verification template * / private static String loginTemplateId; / * * modify password Verification template * / private static String pwdTemplateId Static {try {Properties prop=new Properties (); prop.load (new InputStreamReader (Client.class.getClassLoader (). GetResourceAsStream ("sms.properties"), "gbk")); secretId = prop.get ("secretId"). ToString (); secretKey = prop.get ("secretKey"). ToString (); appId = prop.get ("appId"). ToString () AppKey = prop.get ("appKey"). ToString (); smsSign = prop.get ("smsSign"). ToString (); expireTime = prop.get ("expireTime"). ToString (); loginTemplateId = prop.get ("loginTemplateId"). ToString (); pwdTemplateId = prop.get ("pwdTemplateId"). ToString ();} catch (Exception e) {e.printStackTrace () }} / * SMS sending tool class * @ param phoneNumbers send mobile phone number * @ param templateParamSet template parameters * / public static SendStatus sendMessage (String [] phoneNumbers, String [] templateParamSet,Integer type) throws TencentCloudSDKException {/ / instantiate an authentication object. Input parameters need to pass Tencent Cloud account key pair secretId,secretKey Credential cred = new Credential (secretId,secretKey) / / instantiate a http option HttpProfile httpProfile = new HttpProfile (); httpProfile.setReqMethod ("POST"); httpProfile.setConnTimeout (60); / / instantiate a client configuration object, specify timeout, etc. Configuration ClientProfile clientProfile = new ClientProfile () / * SDK uses TC3-HMAC-SHA256 for signature by default * do not modify this field * / clientProfile.setSignMethod ("HmacSHA256"); clientProfile.setHttpProfile (httpProfile); SmsClient client = new SmsClient (cred, "ap-guangzhou", clientProfile); SendSmsRequest req = new SendSmsRequest (); / / SMS application ID req.setSmsSdkAppId (appId) / / SMS signature content req.setSignName (smsSign); / * template ID: the approved template ID is required. Template ID can log in to [SMS console] to view * / / use different templates according to different businesses: switch (type) {case 1: req.setTemplateId (pwdTemplateId); break; case 2: req.setTemplateId (loginTemplateId); break } / / send mobile phone number req.setPhoneNumberSet (phoneNumbers); / * template parameter: if there is no template parameter, set it to empty * / req.setTemplateParamSet (templateParamSet); SendSmsResponse res = client.SendSms (req); return res.getSendStatusSet () [0] } / * randomly generate verification code for specified length SMS * * @ param smsLengthEnum SMS verification code length enumeration * @ return random verification code * / public static String createSmsRandomCode (SmsLengthEnum smsLengthEnum) {return RandomUtil.randomNumbers (smsLengthEnum.getLength ()) } / * create SMS verification code, cache key policy * policy: prefix _ business name _ mobile number * * @ param prefix prefix * @ param phone mobile number * @ param businessStr business name * @ return SMS verification code Cache key Policy * / public static String createSmsCacheKey (String prefix, String phone, String businessStr) {return prefix + "_" + businessStr + "_" + phone }} 3. Business layer
Use redis to store randomly generated CAPTCHA and destroy it after use
Package com.cloud.system.api.service.impl; import com.cloud.system.api.bean.SmsPwdBean;import com.cloud.system.api.bean.SmsResult;import com.cloud.system.api.entity.HumanInfo;import com.cloud.system.api.entity.SmsRecord;import com.cloud.system.api.enums.SmsLengthEnum;import com.cloud.system.api.enums.SmsTypeEnum;import com.cloud.system.api.mapper.HumanInfoMapper;import com.cloud.system.api.mapper.SmsRecordMapper;import com.cloud.system.api.service.SmsRecordService Import com.cloud.system.common.utils.RedisUtil;import com.cloud.system.common.utils.RegUtils;import com.cloud.system.common.utils.SmsUtil;import com.tencentcloudapi.common.exception.TencentCloudSDKException;import com.tencentcloudapi.sms.v20210111.models.SendStatus;import lombok.extern.slf4j.Slf4j;import org.apache.commons.codec.digest.DigestUtils;import org.apache.commons.collections.CollectionUtils;import org.apache.commons.lang.StringUtils;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional Import tk.mybatis.mapper.entity.Example; import javax.annotation.Resource;import java.util.Date;import java.util.List;import java.util.Objects; / * * @ program: server-master * @ description: * @ author: hjq * @ create: 2021-12-20 * / @ Service ("smsRecordService") @ Transactional@Slf4jpublic class SmsRecordServiceImpl implements SmsRecordService {@ Resource private SmsRecordMapper smsRecordMapper; @ Resource private HumanInfoMapper humanInfoMapper; @ Resource private RedisUtil redisUtil @ Override public SmsResult getModPwdVcode (String humanName, String phoneNumber) {/ / first check the mobile number if (! RegUtils.isMoblie (phoneNumber)) return new SmsResult (500,500, "the mobile number is not in the correct format!" , null); / / then verify whether the input mobile phone number is bound to the user name if (humanInfoMapper.existByNameAndPhone (humanName, phoneNumber) = 6 & & password.length ()
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.