阿里云发送短信

本文档介绍了如何创建一个配置类来存储阿里云短信服务的AK和模板信息,并提供了读取配置文件的`CommonConfig`类。此外,还展示了一个名为`SMSUtils`的工具类,该类用于发送短信,依赖于`CommonConfig`获取配置并利用阿里云SDK发送实际的短信。工具类中包含了一个`sendSms`方法,接收手机号、模板代码和验证码,通过阿里云SDK发送短信并返回发送结果。
创建一个配置类存储阿里云的一些配置
############### 阿里云短信发送 需要修改  ######################################
# 阿里云AK账号 临时使用 需要修改
config.access_key_id=******************************
# 阿里云AK密码 临时使用 需要修改
config.secret=******************************
# 短信签名 临时使用 需要修改
config.sign_name=******************************
# 用户使用的模板code 临时使用 需要修改
config.sms_template_code=******************************
写一个读取配置文件的配置类
package com.lemon.move.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

/**
 * @author Zhm
 * @date 2021/5/17 9:07
 **/
@Data
@Component
@PropertySource("classpath:config.properties")
@ConfigurationProperties(prefix = "config")
public class CommonConfig {

    /*******************短信************************/
    /*阿里云AK账号*/
    private String access_key_id;
    /*阿里云AK密码*/
    private String secret;
    /*短信签名*/
    private String sign_name;
    /*用户使用的模板code*/
    private String sms_template_code;
}
写一个发送短信的工具类
package com.lemon.move.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.lemon.move.config.CommonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 短信发送工具类
 * 给固定值得配置都是阿里云的配置无需修改,需要修改的配置在config.properties文件中
 **/
@Component
public class SMSUtils {

    @Autowired
    CommonConfig commonConfig;

 	/**
     * 发送短信
     * @param phoneNum      手机号
     * @param TemplateCode  短信模板code
     * @param code          验证码
     * @return              true:成功  false:失败
     */
    public boolean sendSms(String phoneNum, String TemplateCode, String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",commonConfig.getAccess_key_id(),commonConfig.getSecret());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");

        //自定义信息
        request.putQueryParameter("PhoneNumbers", phoneNum); //发送至手机号
        request.putQueryParameter("SignName", commonConfig.getSign_name());  //自己配置的短信签名
        request.putQueryParameter("TemplateCode", TemplateCode); //自己配置的模板 模版CODE
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code", code);
        //构建一个短信验证码
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();  //这个是他自带的判断他发送的是否成功,成功就直接返回
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lemon20120331

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

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

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

打赏作者

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

抵扣说明:

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

余额充值