java腾讯短信接口调用(含语音短信)

依赖

<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>com.github.qcloudsms</groupId>

  <artifactId>qcloudsms</artifactId>

  <version>1.0.5</version>

  <name>qcloud sms java sdk</name>

  <url>https://github.com/qcloudsms/qcloudsms_java/</url>

  <description>this Repository is responsible for qlcoud sms sdk</description>

  <licenses>

    <license>

      <name>MIT License</name>

      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>

    </license>

  </licenses>

 

  <developers>

    <developer>

      <name>qcloudsms</name>

      <email>qcloudsms@gmail.com</email>

      <organization>tencent</organization>

      <organizationUrl>https://cloud.tencent.com</organizationUrl>

    </developer>

  </developers>

 

  <scm>

    <connection>scm:git:git://github.com/qcloudsms/qcloudsms_java.git</connection>

    <developerConnection>scm:git:git://github.com/qcloudsms/qcloudsms_java.git</developerConnection>

    <url>https://github.com/qcloudsms/qcloudsms_java/blob/master/LICENSE</url>

  </scm>

 

  <distributionManagement>

    <snapshotRepository>

      <id>ossrh</id>

      <url>https://oss.sonatype.org/content/repositories/snapshots</url>

    </snapshotRepository>

    <repository>

      <id>ossrh</id>

      <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>

    </repository>

  </distributionManagement>

 

  <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>org.json</groupId>

      <artifactId>json</artifactId>

      <version>20170516</version>

    </dependency>

 

    <dependency>

      <groupId>org.apache.httpcomponents</groupId>

      <artifactId>httpclient</artifactId>

      <version>4.5.3</version>

    </dependency>

 

    <dependency>

      <groupId>org.apache.httpcomponents</groupId>

      <artifactId>httpmime</artifactId>

      <version>4.5.3</version>

    </dependency>

 

    <dependency>

      <groupId>commons-codec</groupId>

      <artifactId>commons-codec</artifactId>

      <version>1.11</version>

      <scope>compile</scope>

    </dependency>

  </dependencies>

 

 

  <build>

    <plugins>

      <plugin>

        <groupId>org.sonatype.plugins</groupId>

        <artifactId>nexus-staging-maven-plugin</artifactId>

        <version>1.6.7</version>

        <extensions>true</extensions>

        <configuration>

          <serverId>ossrh</serverId>

          <nexusUrl>https://oss.sonatype.org/</nexusUrl>

          <autoReleaseAfterClose>true</autoReleaseAfterClose>

        </configuration>

      </plugin>

      <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-source-plugin</artifactId>

        <version>2.2.1</version>

        <executions>

          <execution>

            <id>attach-sources</id>

            <goals>

              <goal>jar-no-fork</goal>

            </goals>

          </execution>

        </executions>

      </plugin>

      <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-javadoc-plugin</artifactId>

        <version>2.9.1</version>

        <executions>

          <execution>

            <id>attach-javadocs</id>

            <goals>

              <goal>jar</goal>

            </goals>

          </execution>

        </executions>

      </plugin>

      <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-gpg-plugin</artifactId>

        <version>1.5</version>

        <executions>

          <execution>

            <id>sign-artifacts</id>

            <phase>verify</phase>

            <goals>

              <goal>sign</goal>

            </goals>

          </execution>

        </executions>

      </plugin>

      <plugin>

        <artifactId>maven-assembly-plugin</artifactId>

        <configuration>

          <appendAssemblyId>false</appendAssemblyId>

          <descriptorRefs>

            <descriptorRef>jar-with-dependencies</descriptorRef>

          </descriptorRefs>

        </configuration>

      </plugin>

    </plugins>

  </build>

 

</project>

 

 

 

 

编写方法

package com.tesst;

 

import com.github.qcloudsms.SmsSingleSender;

import com.github.qcloudsms.SmsSingleSenderResult;

import com.github.qcloudsms.SmsMultiSender;

import com.github.qcloudsms.SmsMultiSenderResult;

import com.github.qcloudsms.SmsStatusPuller;

import com.github.qcloudsms.SmsMobileStatusPuller;

import com.github.qcloudsms.SmsStatusPullCallbackResult;

import com.github.qcloudsms.SmsStatusPullReplyResult;

import com.github.qcloudsms.SmsVoiceVerifyCodeSender;

import com.github.qcloudsms.SmsVoiceVerifyCodeSenderResult;

import com.github.qcloudsms.SmsVoicePromptSender;

import com.github.qcloudsms.SmsVoicePromptSenderResult;

import com.github.qcloudsms.httpclient.HTTPException;

 

import org.json.JSONException;

 

import java.io.IOException;

 

 

public class QcloudSmsTest {

 

    public static void main(String[] args) {

        // 短信应用SDK AppID

        int appid = 1400135511; // 1400开头

 

        // 短信应用SDK AppKey

        String appkey = "47b0542970d8c6acdc66b260b853bebb";

 

        // 需要发送短信的手机号码

        String[] phoneNumbers = {"18003815237"};

 

        // 短信模板ID,需要在短信应用中申请

        // NOTE: 这里的模板ID`7839`只是一个示例,

        // 真实的模板ID需要在短信控制台中申请

        int templateId = 185411;

 

        // 签名

        // NOTE: 这里的签名"腾讯云"只是一个示例,

        // 真实的签名需要在短信控制台中申请,另外

        // 签名参数使用的是`签名内容`,而不是`签名ID`

        String smsSign = "";

 

        // 单发短信

    /*    try {

            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);

            SmsSingleSenderResult result = ssender.send(0, "86", phoneNumbers[0],

                "【腾讯云】您的验证码是: 5678", "", "");

            System.out.print(result);

        } catch (HTTPException e) {

            // HTTP响应码错误

            e.printStackTrace();

        } catch (JSONException e) {

            // json解析错误

            e.printStackTrace();

        } catch (IOException e) {

            // 网络IO错误

            e.printStackTrace();

        }*/

 

        // 指定模板ID单发短信

  /*      try {

            String[] params = {"5678","1"};

            SmsMultiSender msender = new SmsMultiSender(appid, appkey);

            SmsMultiSenderResult result =  msender.sendWithParam("86", phoneNumbers,

                templateId, params, smsSign, "", "");  // 签名参数未提供或者为空时,会使用默认签名发送短信

            System.out.print(result);

        } catch (HTTPException e) {

            // HTTP响应码错误

            e.printStackTrace();

        } catch (JSONException e) {

            // json解析错误

            e.printStackTrace();

        } catch (IOException e) {

            // 网络IO错误

            e.printStackTrace();

        }*/

 

        // 发送语音验证码

        try {

            SmsVoiceVerifyCodeSender vvcsender = new SmsVoiceVerifyCodeSender(appid,appkey);

            SmsVoiceVerifyCodeSenderResult result = vvcsender.send("86", phoneNumbers[0],

                "520131466666666666", 2, "");

            System.out.print(result);

        } catch (HTTPException e) {

            // HTTP响应码错误

            e.printStackTrace();

        } catch (JSONException e) {

            // json解析错误

            e.printStackTrace();

        } catch (IOException e) {

            // 网络IO错误

            e.printStackTrace();

        }

 

        // 发送语音通知

      /*  try {

            SmsVoicePromptSender vpsender = new SmsVoicePromptSender(appid, appkey);

            SmsVoicePromptSenderResult result = vpsender.send("86", phoneNumbers[0],

                2, 2, "5678", "");

            System.out.print(result);

        } catch (HTTPException e) {

            // HTTP响应码错误

            e.printStackTrace();

        } catch (JSONException e) {

            // json解析错误

            e.printStackTrace();

        } catch (IOException e) {

            // 网络IO错误

            e.printStackTrace();

        }*/

 

        // 拉取短信回执以及回复

       /* try {

            // Note: 短信拉取功能需要联系腾讯云短信技术支持(QQ:3012203387)开通权限

            int maxNum = 10;  // 单次拉取最大量

            SmsStatusPuller spuller = new SmsStatusPuller(appid, appkey);

 

            // 拉取短信回执

            SmsStatusPullCallbackResult callbackResult = spuller.pullCallback(maxNum);

            System.out.println(callbackResult);

 

            // 拉取回复

            SmsStatusPullReplyResult replyResult = spuller.pullReply(maxNum);

            System.out.println(replyResult);

        } catch (HTTPException e) {

            // HTTP响应码错误

            e.printStackTrace();

        } catch (JSONException e) {

            // json解析错误

            e.printStackTrace();

        } catch (IOException e) {

            // 网络IO错误

            e.printStackTrace();

        }*/

 

        // 拉取单个手机短信状态

       /* try {

            int beginTime = 1511125600;  // 开始时间(unix timestamp)

            int endTime = 1511841600;    // 结束时间(unix timestamp)

            int maxNum = 10;             // 单次拉取最大量

            SmsMobileStatusPuller mspuller = new SmsMobileStatusPuller(appid, appkey);

 

            // 拉取短信回执

            SmsStatusPullCallbackResult callbackResult = mspuller.pullCallback("86",

                phoneNumbers[0], beginTime, endTime, maxNum);

            System.out.println(callbackResult);

 

            // 拉取回复

            SmsStatusPullReplyResult replyResult = mspuller.pullReply("86",

                phoneNumbers[0], beginTime, endTime, maxNum);

            System.out.println(replyResult);

        } catch (HTTPException e) {

            // HTTP响应码错误

            e.printStackTrace();

        } catch (JSONException e) {

            // json解析错误

            e.printStackTrace();

        } catch (IOException e) {

            // 网络IO错误

            e.printStackTrace();

        }*/

    }

}

 

 

转载于:https://www.cnblogs.com/chengxiaolong/p/10199555.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值