引入依赖
< dependency> < groupId> com.tencentcloudapi</ groupId> < artifactId> tencentcloud-sdk-java</ artifactId> < version> 3.1.270</ version>
</ dependency>
配置文件
sms : tencentcloud : enabled : true region-id : xxxapp-id : xxxsecret-id : xxxsecret-key : xxxsign-name : xxxtemplate-id : xxx
属性类
package com. qiangesoft. sms. tencentcloud. config ; import lombok. Data ;
import org. springframework. boot. context. properties. ConfigurationProperties ;
@Data
@ConfigurationProperties ( prefix = "sms.tencentcloud" )
public class TencentcloudSmsProperties { private boolean enabled = true ; private String regionId; private String appId; private String secretId; private String secretKey; private String signName; private String templateId; }
配置类
package com. qiangesoft. sms. tencentcloud. config ; import com. tencentcloudapi. common. Credential ;
import com. tencentcloudapi. common. profile. ClientProfile ;
import com. tencentcloudapi. common. profile. HttpProfile ;
import com. tencentcloudapi. sms. v20210111. SmsClient ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. boot. autoconfigure. condition. ConditionalOnMissingBean ;
import org. springframework. boot. autoconfigure. condition. ConditionalOnProperty ;
import org. springframework. boot. context. properties. EnableConfigurationProperties ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
@Configuration
@EnableConfigurationProperties ( TencentcloudSmsProperties . class )
@ConditionalOnProperty ( prefix = "sms.tencentcloud" , name = "enabled" , havingValue = "true" )
public class TencentcloudSmsConfiguration { private static final String ENDPOINT = "sms.tencentcloudapi.com" ; @Autowired private TencentcloudSmsProperties tencentcloudSmsProperties; @Bean @ConditionalOnMissingBean public SmsClient smsClient ( ) { Credential cred = new Credential ( tencentcloudSmsProperties. getSecretId ( ) , tencentcloudSmsProperties. getSecretKey ( ) ) ; HttpProfile httpProfile = new HttpProfile ( ) ; httpProfile. setReqMethod ( "POST" ) ; httpProfile. setConnTimeout ( 60 ) ; httpProfile. setEndpoint ( ENDPOINT ) ; ClientProfile clientProfile = new ClientProfile ( ) ; clientProfile. setSignMethod ( "HmacSHA256" ) ; clientProfile. setHttpProfile ( httpProfile) ; return new SmsClient ( cred, tencentcloudSmsProperties. getRegionId ( ) , clientProfile) ; } }
短信发送实现
package com. qiangesoft. sms. tencentcloud. handler ; import com. qiangesoft. sms. tencentcloud. config. TencentcloudSmsProperties ;
import com. tencentcloudapi. common. exception. TencentCloudSDKException ;
import com. tencentcloudapi. sms. v20210111. SmsClient ;
import com. tencentcloudapi. sms. v20210111. models. SendSmsRequest ;
import com. tencentcloudapi. sms. v20210111. models. SendSmsResponse ;
import lombok. RequiredArgsConstructor ;
import lombok. extern. slf4j. Slf4j ;
import org. springframework. stereotype. Service ;
@Slf4j
@RequiredArgsConstructor
@Service
public class TencentcloudSendHandler { private final SmsClient smsClient; private final TencentcloudSmsProperties tencentcloudSmsProperties; public boolean send ( String [ ] phones, String [ ] params) { return this . send ( phones, tencentcloudSmsProperties. getTemplateId ( ) , params) ; } public boolean send ( String [ ] phones, String templateId, String [ ] params) { SendSmsRequest req = new SendSmsRequest ( ) ; req. setSmsSdkAppId ( tencentcloudSmsProperties. getAppId ( ) ) ; req. setSignName ( tencentcloudSmsProperties. getSignName ( ) ) ; req. setTemplateId ( templateId) ; req. setTemplateParamSet ( params) ; req. setPhoneNumberSet ( phones) ; try { SendSmsResponse res = smsClient. SendSms( req) ; } catch ( TencentCloudSDKException e) { throw new RuntimeException ( e) ; } return false ; } }
测试
package com. qiangesoft. sms. tencentcloud. controller ; import com. qiangesoft. sms. tencentcloud. handler. TencentcloudSendHandler ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ;
@RequestMapping ( "/tsms" )
@RestController
public class TSmsController { @Autowired private TencentcloudSendHandler tencentcloudSendHandler; @GetMapping ( "/send" ) public Boolean send ( ) { String [ ] params = { "张三" , "23" } ; return tencentcloudSendHandler. send ( new String [ ] { "15883968757" } , params) ; } }