java整合阿里云短信服务

先去阿里云平台申请短信签名和短信模版,这个可以去网上找教程,非常简单

先导入依赖

  <!--引入阿里云短信服务-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibabacloud-ecs20140526</artifactId>
            <version>2.0.5</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dysmsapi20170525</artifactId>
            <version>2.0.24</version>
        </dependency>

使用短信发送工具类

 
/**
 * 阿里云短信服务
 */
public class Samples {

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static 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 = "请参考 https://api.aliyun.com/product/Dysmsapi";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * 发送短信
     *
     * @param mobile
     * @return
     * @throws Exception
     */
    public static Result<Object> Samples(String mobile, String code) throws Exception {
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html
        com.aliyun.dysmsapi20170525.Client client = Samples.createClient("您的 AccessKey ID"," 您的 AccessKey Secret");
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setTemplateParam("{\"code\":" + code + "}")
                .setTemplateCode("您的模版CODE")
                .setPhoneNumbers(mobile)
                .setSignName("您的模版签名");
        try {
            SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, new com.aliyun.teautil.models.RuntimeOptions());
            return Result.error("验证码发送成功,(五分钟内有效)");
        } catch (TeaException error) {
            com.aliyun.teautil.Common.assertAsString(error.message);
            error.printStackTrace();
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);

            com.aliyun.teautil.Common.assertAsString(error.message);
            _error.printStackTrace();
        }
        return Result.error("短信发送失败");
    }

}

 调用短信服务

  @Test
    public void dxyzm() throws Exception {
        String code = "1234";//验证码//您可以设置为随机数,这里只是简单演示
        String phone ="18888888888";//手机号码
        Samples.Samples(phone, code);   //使用阿里云短信服务
        System.out.println("发送成功:"+code);
    }

看效果

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可碧-牢大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值