对接阿里短信平台

package com.ocsmarter.alarm.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

// 测试短信工具类
public class TestSMSUtil {
    // 签名
    private static final String signName = "数字科技";

    // 模板
    private static final String templateCode = "SMS_297961352";

    // 阿里云短信配置信息
    private static final String accessKeyId = "LTAI5tPnXjhzMyLfhSx*";
    private static final String accessKeySecret = "fNmjfvbMGPiJcjx81gcY0*";
    private static final String REGION_ID = "cn-hangzhou";
    private static final String PRODUCT = "Dysmsapi";
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";

    /**
     * 发送短信通知
     *
     * @param mobile 手机号
     * @param code 验证码
     * @return 执行结果
     */
    public static boolean sendSMS(String mobile, String code) {
        try {
            IClientProfile profile = DefaultProfile.getProfile(REGION_ID, accessKeyId, accessKeySecret);

            DefaultProfile.addEndpoint(REGION_ID, REGION_ID, PRODUCT, DOMAIN);

            IAcsClient acsClient = new DefaultAcsClient(profile);

            SendSmsRequest request = new SendSmsRequest();

            request.setMethod(MethodType.POST);

            // 手机号可以单个也可以多个(多个用逗号隔开,如:15*******13,13*******27,17*******56)
            request.setPhoneNumbers(mobile);

            request.setSignName(signName);

            request.setTemplateCode(templateCode);

            /*  例如签名内容为:某某公司
            例如模板内容为:亲爱的同事,很高兴的通知您,您抽中了由领导${userName}派发的大饼奖励${money}元,请及时找财务领取!
            变量属性:userName-其他;money-其他;
            则短信内容为:【某某公司】 亲爱的同事,很高兴的通知您,您抽中了由领导${userName}派发的大饼奖励${money}元,请及时找财务领取!*/
            request.setTemplateParam("{\"code\":\""+ code +"\"}");

            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            if ((sendSmsResponse.getCode() != null) && (sendSmsResponse.getCode().equals("OK"))) {
                System.out.println("发送成功,code:" + sendSmsResponse.getCode());
                return true;
            } else {
                System.out.println("发送失败,code:" + sendSmsResponse.getCode());
                return false;
            }
        } catch (ClientException e) {
            System.out.println("发送失败,系统错误!");
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println(TestSMSUtil.sendSMS("18810557753", "5678"));
    }

}

阿里短信对接需要以下步骤: 1. 在阿里短信控制台中创建签名和模板,获取对应的签名和模板CODE。 2. 在阿里云控制台中获取 AccessKey ID 和 AccessKey Secret。 3. 在代码中调用阿里短信 API,传入必要的参数,例如短信模板、签名、接收手机号等。 这里提供一个简单的 PHP 代码示例: ```php <?php require_once './aliyun-php-sdk-core/Config.php'; use Aliyun\Core\DefaultAcsClient; use Aliyun\Core\Profile\DefaultProfile; use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest; // 替换成自己的AccessKey信息 $accessKeyId = "your_accessKeyId"; $accessKeySecret = "your_accessKeySecret"; // 设置阿里短信服务所在的Region,如华东1 $region = "cn-hangzhou"; // 替换成自己的短信签名和模板CODE $signName = "your_signName"; $templateCode = "your_templateCode"; // 发送短信的手机号码,支持多个手机号码,用英文逗号分隔 $phoneNumbers = "your_phoneNumbers"; // 短信模板中的变量替换JSON串,如验证码为:{"code":"123456"} $templateParam = "{\"code\":\"123456\"}"; // 初始化DefaultAcsClient实例并设置参数 $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret); DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms", "sms.aliyuncs.com"); $client = new DefaultAcsClient($profile); $request = new SendSmsRequest(); // 设置请求参数 $request->setPhoneNumbers($phoneNumbers); $request->setSignName($signName); $request->setTemplateCode($templateCode); $request->setTemplateParam($templateParam); // 发送短信 $response = $client->getAcsResponse($request); // 输出结果 print_r($response); ?> ``` 注意,以上代码中需要替换成自己的 AccessKey ID、AccessKey Secret、短信签名、模板 CODE、手机号码、短信模板变量等信息。另外,阿里短信服务需要收取一定的费用,请确保账户余额充足。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值