java发送短信验证码《工具类》

1、短信的签名

就是申请短信服务的平台是做什么的?

2、短信的模板

尊敬的${name},感谢您的注册,您的MT4账号:${account},密码:${password},请到官网下载软件。

3、发送信息

pom.xml


        
            com.aliyun
            aliyun-java-sdk-core
            4.5.3
        

ShortMes.java

package com.woniu.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 尊敬的${name},感谢您的注册,您的MT4账号:${account},密码:${password},请到官网下载软件。
 * 我们给阿里返送下面 这个json,阿里按照json填充到上面模板,再发送给用户
 * {"name":13333333333,"account":"1333333333","password":"3232"}
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ShortMes {
    private String name;//可以为手机号
    private String account;//充当手机号
    private String password;//充当验证码
}

SMSUtils

package com.woniu.utils;

import com.alibaba.fastjson.JSON;
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.woniu.entity.ShortMes;

import java.io.IOException;

public class SMSUtils {

    public static void send(ShortMes shortMes) throws IOException {

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tKSK6vEeTHY5Mzvd9yN", "TPGxhbOWtmeQxwAlrMP97s8PoROreR");
        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", shortMes.getAccount());//往什么手机号发
        request.putQueryParameter("SignName", "MT4账号");
        request.putQueryParameter("TemplateCode", "SMS_157449478");
        request.putQueryParameter("TemplateParam", JSON.toJSONString(shortMes));//发送的内容
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {

        ShortMes shortMes = new ShortMes("13260959680","13260959680","3297");
        send(shortMes);

        //String s = "^(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[5-7|9])|(?:5[0-3|5-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[1|8|9]))\\d{8}$";
        //System.out.println("14443431566".matches(s));
    }
}

java发送短信验证码《工具类》_第1张图片

你可能感兴趣的:(为你撰写的故事,java,开发语言)