集成阿里短信

添加依赖

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>

工具类

package ly.happy;

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.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.HashMap;
import java.util.Map;

public class AliyunSmsUtil {
    /**
     * 产品名称:云通信短信API产品(不用更换)
     */
    private static final String PRODUCT = "Dysmsapi";

    /**
     * 产品域名(不用更换)
     */
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";

    /**
     * todo 开发者自己的AK(在阿里云访问控制台寻找)
     */
    private static final String ACCESS_KEY_ID = "xxx";

    /**
     * todo accessKeySecret(在阿里云访问控制台寻找)
     */
    private static final String ACCESS_KEY_SECRET = "xxx";

    /**
     * todo 必填:短信签名(签名管理中的签名名称)
     */
    private static final String SIGN_NAME = "xxx";

    /**
     * todo 必填:REGION_ID(不用更换)
     */
    private static final String REGION_ID = "cn-hangzhou";

    /**
     * todo 必填:模板CODE(模板管理中的模板CODE)
     */
    public static final String LOGIN_TEMPLATE_CODE = "xxx";

    /**
     * 发送方法
     *
     * @param phone         电话号码
     * @param templateCode  模板编号
     * @param templateParam 模板中的参数
     * @return 返回值
     * @throws ClientException 异常
     */
    public static SendSmsResponse sendSms(String phone, String templateCode, String templateParam) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        DefaultProfile.addEndpoint(REGION_ID, REGION_ID, PRODUCT, DOMAIN);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phone);
        request.setSignName(SIGN_NAME); // 短信签名
        request.setTemplateCode(templateCode);
        request.setTemplateParam(templateParam);

        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("10000");

        //hint 此处可能会抛出异常,注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        return sendSmsResponse;
    }

    public static void main(String[] args) throws ClientException, InterruptedException {

        Map<String, String> paramMap = new HashMap<String, String>();
        //map.put("name",name);
        String code = createRandomVcode();
        paramMap.put("code", code);
        String templateParam = com.alibaba.fastjson.JSONObject.toJSONString(paramMap);
        System.out.println(templateParam);

        String phones = "xxx";
        SendSmsResponse response = sendSms(phones, LOGIN_TEMPLATE_CODE, templateParam);
        System.out.println("短信接口返回的数据----------------");
        System.out.println("Code=" + response.getCode());
        System.out.println("Message=" + response.getMessage());
        System.out.println("RequestId=" + response.getRequestId());
        System.out.println("BizId=" + response.getBizId());
    }

    /**
     * 生成六位随机数
     *
     * @return
     */
    public static String createRandomVcode() {
        //验证码
        StringBuilder vcode = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            vcode.append((int) (Math.random() * 9));
        }
        return vcode.toString();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值