小牛云短信发送JAVA版

小牛云短信发送JAVA版

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>sms</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-dysmsapi -->
        <!-- 小牛短信,底层也是阿里的 -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>2.8.3</version>
        </dependency>


        <!-- 接入阿里短信 -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.1.0</version>
        </dependency>

    </dependencies>

</project>

java 代码

package message;

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

/*大数据集群运行监控短信告警*/

public class SendMessage {

    private static final String phones = "18088996991";

    //产品名称:云通信短信API产品,开发者无需替换
    private static final String product = "Dysmsapxi";
    //产品域名,开发者无需替换
    private static final String domain = "sms11.hzgxxr.com:400x81";
    private static final String accessKeyId = "XNY2_IU4=A+xHVm4xyl0=i4hbeQ==";
    private static final String accessKeySecret = "hi3NkHFciPQ=IdxQYxkW95Q=";
    private static final String regionId = "cn-hangzhou";
    private static final String signName = "今日头条";
    private static final String templateCode = "1024";

    public static void main(String[] args) throws ClientException {
//        String content = args[0];
        String content = "测试 xx";
        sendSms(content);
    }


    public static void sendSms(String content) throws ClientException {
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint(regionId, regionId, product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setMethod(MethodType.POST);
        request.setAcceptFormat(FormatType.JSON);
        request.setPhoneNumbers(phones);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(signName);
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(templateCode);
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        request.setTemplateParam("{ \"content\":\"" + content + "\"}");
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        if (sendSmsResponse != null && sendSmsResponse.getMessage().equals("OK")) {
            System.out.println("发送成功");
        } else {
//            logger.error("sendSms fail,ServerException:" + sendSmsResponse.getMessage());
            System.out.println("发送失败"+sendSmsResponse.getMessage());
        }
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章