利用阿里云发送信息

package dou.aliyun;  
  
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;  
  
public class aliMessageSend {  
      
    // 产品名称:云通信短信API产品,开发者无需替换  
    private static final String product = "Dysmsapi";  
    // 产品域名,开发者无需替换  
    private static final String domain = "dysmsapi.aliyuncs.com";  
  
    // 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)  
    private static String mobile = "目的手机号码";  
    private static String accessKeyId = "服务器控制台自己找";  
    private static String accessKeySecret = "服务器控制台自己找";  
    private static String signName = "若维工作室";  
    private static String templeteCode = "SMS_135195100";  
  
    // 调用短信接口  
    public static void main(String[] args) {  
        try {  
           sendSms();  
        } catch (ClientException e) {  
            System.out.println(e);  
        }  
    }  
  
    // 发送短信方法  
    public static SendSmsResponse sendSms() throws ClientException {  
        // 可自助调整超时时间  
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");  
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");  
  
        // 初始化acsClient,暂不支持region化  
        IClientProfile profile = DefaultProfile.getProfile("cn-beijing", accessKeyId, accessKeySecret);  
        DefaultProfile.addEndpoint("cn-beijing", "cn-beijing", product, domain);  
        IAcsClient acsClient = new DefaultAcsClient(profile);  
  
        // 组装请求对象-具体描述见控制台-文档部分内容  
        SendSmsRequest request = new SendSmsRequest();  
  
        // 必填:待发送手机号  
        request.setPhoneNumbers(mobile);  
        // 必填:短信签名-可在短信控制台中找到  
        request.setSignName(signName);  
        // 必填:短信模板-可在短信控制台中找到  
        request.setTemplateCode(templeteCode);  
  
        // 可选:模板中的变量替换JSON串,如模板内容为"尊敬的用户,您的验证码为${code}"时,此处的值为  
        String jsonParam = "{\"code\":\"shazi\"}";  
        request.setTemplateParam(jsonParam);  
  
        // hint 此处可能会抛出异常,注意catch  
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);  
  
        return sendSmsResponse;  
    }  
  
} 

pom.xml

<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>dou</groupId>
    <artifactId>aliyun</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>aliyun</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>3.2.8</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun.mns</groupId>
            <artifactId>aliyun-sdk-mns</artifactId>
            <version>1.1.8</version>

        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpasyncclient</artifactId>
            <version>4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore-nio</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>
    </dependencies>
</project>

猜你喜欢

转载自www.cnblogs.com/fudou/p/9050554.html
今日推荐