阿里云短信代码

阿里云短信代码示例:

引入SDK依赖:

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.0.3</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
    <!-- 当前最新版本 -->
    <version>1.3.2</version>
</dependency>

发送短信:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sms.model.v20170525.SendSmsRequest;
import com.aliyuncs.sms.model.v20170525.SendSmsResponse;

public class SmsUtil {

    public static void main(String[] args) {
        String accessKeyId = "your_access_key_id"; // 阿里云AccessKeyId
        String accessKeySecret = "your_access_key_secret"; // 阿里云AccessKeySecret
        String mobile = "13888888888"; // 接收短信的手机号码
        String signName = "短信签名"; // 短信签名
        String templateCode = "短信模板Code"; // 短信模板Code
        String templateParam = "{"code":"123456"}"; // 短信模板中的参数,以json格式传递

        try {
            SendSmsResponse response = sendSms(accessKeyId, accessKeySecret, mobile, signName, templateCode, templateParam);
            System.out.println("短信发送结果:" + response.getCode());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送短信
     *
     * @param accessKeyId     阿里云AccessKeyId
     * @param accessKeySecret 阿里云AccessKeySecret
     * @param mobile          接收短信的手机号码
     * @param signName        短信签名
     * @param templateCode    短信模板Code
     * @param templateParam   短信模板中的参数,以json格式传递
     * @return SendSmsResponse
     * @throws ClientException 阿里云SDK异常
     */
    public static SendSmsResponse sendSms(String accessKeyId, String accessKeySecret, String mobile, String signName, String templateCode, String templateParam) throws ClientException {
        // 创建DefaultAcsClient实例并初始化
        IAcsClient acsClient = createDefaultAcsClient(accessKeyId, accessKeySecret);

        // 创建发送短信的请求
        SendSmsRequest request = createSendSmsRequest(mobile, signName, templateCode, templateParam);

        // 发送短信并获取响应
        SendSmsResponse response;
        try {
            response = acsClient.getAcsResponse(request);
        } catch (ServerException e) {
            throw new ClientException(e.getMessage(), e.getErrCode(), e.getRequestId());
        } catch (ClientException e) {
            throw e;
        }

        return response;
    }

    /**
     * 创建DefaultAcsClient实例并初始化
     *
     * @param accessKeyId     阿里云AccessKeyId
     * @param accessKeySecret 阿里云AccessKeySecret
     * @return DefaultAcsClient
     */
    private static IAcsClient createDefaultAcsClient(String accessKeyId, String accessKeySecret) {
        // 设置RegionId,根据实际情况填写
        String regionId = "cn-hangzhou";

        // 初始化Profile
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);

        // 创建DefaultAcsClient实例
        return new DefaultAcsClient(profile);
    }

    /**
     * 创建发送短信的请求
     *
     * @param mobile        接收短信的手机号码
     * @param signName      短信签名
     * @param templateCode  短信模板Code
     * @param templateParam 短信模板中的参数,以json格式传递
     * @return SendSmsRequest
     */
    private static SendSmsRequest createSendSmsRequest(String mobile, String signName, String templateCode, String templateParam) {
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(mobile); // 设置接收短信的手机号码
        request.setSignName(signName); // 设置短信签名
        request.setTemplateCode(templateCode); // 设置短信模板Code
        request.setTemplateParam(templateParam); // 设置短信模板中的参数

        return request;
    }
}

以上代码示例可以基于阿里云SDK进行短信发送,你需要替换其中的accessKeyIdaccessKeySecretmobilesignNametemplateCodetemplateParam为你自己的实际值。此外,还需要确保你已加入阿里云短信服务,并创建了短信签名和短信模板。

以下是一个简单的示例代码,用于发送短信到指定的手机号码:

阿里云短信代码
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sms.model.v20170525.SendSmsRequest;
import com.aliyuncs.sms.model.v20170525.SendSmsResponse;

public class AliyunSmsExample {

    public static void main(String[] args) {
        String accessKeyId = "your_access_key_id";
        String accessKeySecret = "your_access_key_secret";
        String signName = "your_sign_name";
        String templateCode = "your_template_code";
        String phoneNumber = "your_phone_number";
        
        // 设置超时时间-可自行调整
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        
        // 初始化AcsClient, 暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
        } catch (ClientException e) {
            e.printStackTrace();
        }
        DefaultAcsClient client = new DefaultAcsClient(profile);

        // 组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phoneNumber);
        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        
        // 生成验证码
        String verifyCode = generateVerifyCode();
        
        // 设置短信模板参数
        request.setTemplateParam("{"code":"" + verifyCode + ""}");

        // 发送短信
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            if (response.getCode() != null && response.getCode().equals("OK")) {
                System.out.println("短信发送成功");
            } else {
                System.out.println("短信发送失败,错误码:" + response.getCode() + ",错误信息:" + response.getMessage());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    // 生成四位验证码
    private static String generateVerifyCode() {
        return String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
    }

}

请注意,上述代码中的 your_access_key_idyour_access_key_secretyour_sign_nameyour_template_codeyour_phone_number 分别需要替换为您自己的阿里云 AccessKey ID、AccessKey Secret、短信签名、短信模板和手机号码。

此示例代码使用阿里云SDK的Java版本,您需要在您的项目中引入相关的依赖库。另外,请确保已在阿里云控制台上申请了短信服务,创建了短信签名和短信模板,并开通了短信发送权限。

发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/88544.html

(0)
luotuoemo的头像luotuoemo
上一篇 2023年10月29日 18:49
下一篇 2023年10月29日 19:15

相关推荐

  • 台湾阿里云代理商:apache服务器 启动失败

    可能的原因和解决方法如下: 端口冲突:检查是否有其他程序或服务占用了 Apache 服务器所需的端口(默认为80)。可以通过修改 Apache 配置文件中的 Listen 指令来更改端口号。 配置错误:检查 Apache 配置文件中的语法错误或配置项错误。可以通过运行命令 apachectl -t 或 httpd -t 来检查配置文件的语法是否正确。 权限问…

    2024年1月30日
    29700
  • 阿里云应用服务器镜像

    我在阿里云买了个服务器,能把这个服务器的系统做成镜像下载到本地安装吗? 展开全部不可以,即使是windows系统你强行ghost下载到本地安装也是装不上的,因为系统里只有阿里云的驱动,这不是原版windows server系统,我亲自试过。 求助,阿里云服务器可以自己配置系统镜像不 是可以的,这是基本的功能。。 阿里云windows server2016 n…

    2023年8月28日
    25600
  • 茌平阿里云企业邮箱代理商:购买阿里云服务器有返点吗

    阿里云企业邮箱代理商:购买阿里云服务器有返点吗 作为阿里云企业邮箱的代理商,我们经常接到客户咨询是否在购买阿里云服务器时能够享受返点。在回答这个问题之前,让我们先来了解一下阿里云企业邮箱的优势以及使用企业邮箱后的感受。 阿里云企业邮箱的优势 阿里云企业邮箱是一种云端企业级邮件解决方案,为企业提供稳定、安全、可靠的企业邮箱服务。以下是阿里云企业邮箱的主要优势:…

    2024年1月31日
    69000
  • 新乡阿里云代理商:阿里云datahub

    阿里云datahub是阿里云提供的一种数据集中管理和流式处理的服务。作为阿里云代理商,新乡可以通过与阿里云合作,为客户提供datahub的使用和支持。 阿里云datahub可以帮助用户集中管理多种类型的数据,并提供数据的高可靠性、低延迟和弹性伸缩等特性。它支持多种数据接入方式,包括数据源推送、数据订阅和数据批量导入等,同时还能够支持多种数据消费方式,包括实时…

    2024年1月1日
    26200
  • 阿里云国际站:asp.net网站打包器

    阿里云国际站提供了一个名为“ASP.NET网站打包器”的工具,可以将ASP.NET网站打包成ZIP文件,便于在其他服务器上部署和运行。以下是使用步骤: 登录阿里云控制台,进入“企业应用市场”页面。 在“ASP.NET网站打包器”页面中,点击“立即使用”按钮。 在弹出的创建实例页面中,选择实例规格、地域、网络类型等基本配置信息,然后点击“立即购买”。 实例创建…

    2024年3月8日
    19800

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
购买阿里云服务器请访问:https://www.4526.cn/