对接阿里云短信发送

本文档详细介绍了如何对接阿里云短信服务,包括配置SDK、创建发送短信工具类、实现发送短信功能,以及手机号安全验证和验证码生成流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对接阿里云短信

1.配置sdk:

    <!--阿里云短信SDK-->
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
    </dependency>

2.编写阿里云发送短信工具类

package com.base.cn.platform.os.common.utils.sms;

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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 阿里短信发送工具
 */
public class ALSmsSendUtils {
    private static Logger logger = LoggerFactory.getLogger(ALSmsSendUtils .class);
    private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();


    /**
     * 阿里云短信接口
     *
     * @param mobile	接收人手机号码,多个号码使用','隔开
     * @param content   消息内容: 验证码,提示信息等
     * @param tempId	模板Id
     * @param appKey
     * @param appSecret
     * @return
     */
    static boolean aliSendMessage(String mobile, Map<String,String> content, String tempId, String appKey, String appSecret,String sig) {
        try {
            if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(content) || StringUtils.isEmpty(tempId)
                    || StringUtils.isEmpty(appKey) || StringUtils.isEmpty(appSecret)){
                logger.error("----------发送短信失败: 参数错误");
                return false;
            }
            //初始化ascClient,暂时不支持多region(请勿修改)
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", appKey, appSecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
            IAcsClient acsClient = new DefaultAcsClient(profile);

            //组装请求对象
            SendSmsRequest request = new SendSmsRequest();
            //使用post提交
            request.setMethod(MethodType.POST);
			/* 必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,
			批量调用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值