电脑配置|硬件知识|电脑故障|电脑技巧|电脑应用|组装电脑

阿里云验证码util

阿里短信发送

<!-- 集成阿里云短信验证sdk -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dysmsapi20170525</artifactId>
            <version>2.0.9</version>
        </dependency>

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class SmsSendUtil {

    /* 您的AccessKey ID */
    @Value("${sms.alibaba.accessKeyId}")
    private String accessKeyId;
    /* 您的AccessKey Secret */
    @Value("${sms.alibaba.accessKeySecret}")
    private String accessKeySecret;
    /* 您的签名name */
    @Value("${sms.alibaba.signName}")
    private String signName;
    /* 您的签名code */
    @Value("${sms.alibaba.templateCode}")
    private String templateCode;

    /**
     * 短信发送
     * @param phones
     * @param code
     * @return
     * @throws Exception
     */
    public String sendSms(String phones, String code) throws Exception {
        Config config = new Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phones)
                .setTemplateParam("{\"code\":\"" + code + "\"}");
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse sendSmsResponse = new com.aliyun.dysmsapi20170525.Client(config).sendSms(sendSmsRequest);
        SendSmsResponseBody body = sendSmsResponse.body;
        return body.getCode();
    }
}

上一篇:

下一篇:已经是最新一篇了

发表评论

还没有评论,快来说点什么吧~