Java项目实现发送短信(阿里云)

阿里云短信服务连接: https://help.aliyun.com/document_detail/101300.html

pom.xml文件


        <dependency>
            <groupId>com.aliyungroupId>
            <artifactId>aliyun-java-sdk-coreartifactId>
            <version>4.1.0version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
            <optional>trueoptional>
        dependency>

application.yml文件

sms:
  accessKeyId: 公司或个人在阿里云注册的账户
  accessSecret: 验证用户的密钥
  domain: dysmsapi.aliyuncs.com
  version: 2017-05-25
  action: SendSms
  map:
    SignName: 签名
    TemplateCode: 模板code

读取配置文件,使用bean对象接收值

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

import java.util.Map;

@Data
@ConfigurationProperties(prefix = "sms")
@Component
public class SmsProperties {

    private String accessKeyId;//阿里云短信用户标识

    private String accessSecret;//验证用户密钥

    private String domain;//域

    private String version;//版本号

    private String action;

    private Map<String,String> map;


}

发送短信工具类

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
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.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Configuration
public class TimingTask {
    
    @Autowired
    private SmsProperties smsProperties;

    public  void send(String phoneNum,String corporateName){
        DefaultProfile profile = DefaultProfile.getProfile("default", smsProperties.getAccessKeyId(), smsProperties.getAccessSecret());
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(smsProperties.getDomain());
        request.setVersion(smsProperties.getVersion());
        request.setAction(smsProperties.getAction());
        Map<String, String> map = smsProperties.getMap();
        request.putQueryParameter("PhoneNumbers", phoneNum);//给该手机号发送短信
        request.putQueryParameter("SignName", map.get("SignName"));//短信签名
        request.putQueryParameter("TemplateCode", map.get("TemplateCode"));//短信模板code,配置文件中只需要写模板code不需要写模板内容
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("name",corporateName);
        request.putQueryParameter("TemplateParam",jsonObject.toString());//需要的参数,参数只能使用json格式
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

    }
}

你可能感兴趣的:(项目随笔记)