java发送阿里云短信工具类

开通阿里云平台短信

参考文档:https://help.aliyun.com/zh/sms/developer-reference/sdk-product-overview/?spm=a2c4g.11186623.0.0.180ec71f9R78r9

导包

引入阿里云短信SDK

  <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dysmsapi20170525</artifactId>
            <version>2.0.24</version>
        </dependency>

工具类


@Component
@Slf4j
public class SendSmsUtil {

    //accessKeyId
    @Value("${data.sms.accessKeyId}")
    public String accessKeyId;

    //accessKeySecret
    @Value("${data.sms.accessKeySecret}")
    public String accessKeySecret;

    //签名
    @Value("${data.sms.signName}")
    public String signName;

    //区域 亚太
    public static String endpoint = "dysmsapi.aliyuncs.com";

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填,您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填,您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = endpoint;
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public SmsSendLog sendSms(String templateCode, String phone, Map<String, Object> params) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = createClient(accessKeyId, accessKeySecret);
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phone)
                .setTemplateParam(JSONUtil.toJsonStr(params));

        SmsSendLog smsSendLog = new SmsSendLog();
        smsSendLog.setPhone(phone);
        smsSendLog.setTemplateCode(templateCode);
        smsSendLog.setContent(JSONUtil.toJsonStr(params));
        smsSendLog.setTime(DateUtil.getNow());
        smsSendLog.setCreateTime(DateUtil.getNow());
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
            if (sendSmsResponse.getStatusCode() != null && sendSmsResponse.getStatusCode() == 200) {
                smsSendLog.setRequestId(sendSmsResponse.getBody().getRequestId());
                log.info("发送成功,code:" + sendSmsResponse.getStatusCode());
            } else {
                log.info("发送失败,code:" + sendSmsResponse.getStatusCode());
            }
            smsSendLog.setStatus(sendSmsResponse.getStatusCode());
            smsSendLog.setInfo(sendSmsResponse.getBody().message);
        } catch (TeaException error) {
            // 错误 message
            log.error(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            smsSendLog.setRemark(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            smsSendLog.setRemark(error.message);
        } finally {
            //插入日志
            return smsSendLog;
        }
    }



@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("sms_send_log")
public class SmsSendLog implements Serializable {
    private static final long serialVersionUID = 1L;
    //主键
    @TableId(type = IdType.AUTO)
    private Long id;
    private String phone;
    private String templateCode;
    private String content;
    private Date time;
    private String requestId;
    private Integer status;
    private String info;
    private String remark;
    private Date createTime;

}

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值