阿里云短信服务-Java代码

package com.cac.cc.common;

import com.alibaba.fastjson.JSONObject;
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;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class SendSmsUtil {

    // 签名
    private static final String signName = "中国奥利给公司";

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

    // 阿里云短信配置信息
    private static final String accessKeyId = "LTAI5tJxxxxxxAZ9bucvc";
    private static final String accessKeySecret = "YC3xxxxxxY2GIrc";

    // 服务接入点 华东1(杭州)    cn-hangzhou    dysmsapi.aliyuncs.com
    private static final String REGION_ID = "cn-hangzhou";
    private static final String PRODUCT = "Dysmsapi";

    // 公网接入地址
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";

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

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

            IAcsClient acsClient = new DefaultAcsClient(profile);

            SendSmsRequest request = getSmsRequest(mobile, content,templateCode);

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


    /**
     * 短信发送参数配置
     * @param mobile 手机号
     * @param content 发送内容
     * @param templateCode 模板编号
     * @return SendSmsRequest
     */

    private static SendSmsRequest getSmsRequest(String mobile, JSONObject content,String templateCode) {
        SendSmsRequest request = new SendSmsRequest();

        request.setMethod(MethodType.POST);

        // 手机号可以单个也可以多个(多个用逗号隔开,如:15*******13,13*******27,17*******56)
        request.setPhoneNumbers(mobile);
        // 签名名称
        request.setSignName(signName);
        // 模板
        request.setTemplateCode(templateCode);
        //  发送内容

        request.setTemplateParam(content.toString());
        return request;
    }

    public static void main(String[] args) {
        String code="张三";
        JSONObject jsonObject = new JSONObject();
        //jsonObject.put("name",code);
       // jsonObject.put("time","2024年05月10日");
        //jsonObject.put("time1","2025年05月10日");
        boolean b = SendSmsUtil.sendSMS("1738xxxx3,15xxx7,186xxx2546", jsonObject,"SMS_465901379");
        System.out.println(b);
    }



    /**
     * 获取逗号分隔的拼接字符串
     *
     * @param str 用于拼接的字符串集合
     * @return String
     */
    public static String getSplitString(List<String> str) {
        StringBuilder newS = new StringBuilder();
        if (str != null && !str.isEmpty()) {
            for (String s : str) {
                newS.append(s).append(",");
            }
        }
        if (!newS.isEmpty())
            newS.deleteCharAt(newS.length() - 1);// 删除最后一个多余的逗号
        return newS.toString();
    }


}



// 这里是你们业务代码

// 注册发送短信
SendSmsUtil.sendSMS(user.getPhone(),jsonObject,templateCode);


<!-- 阿里云短信服务 -->

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>3.2.6</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
    <version>1.0.0</version>
</dependency>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值