阿里云短信代码

阿里云短信代码示例:

引入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

相关推荐

  • 绍兴阿里云代理商:按占用空间排序

    以下是绍兴地区的阿里云代理商按占用空间排序的列表: 域云网络科技有限公司:作为一家专业的阿里云代理商,域云网络科技有限公司提供各类阿里云产品和解决方案,拥有大量占用空间。 绍兴佰联信息技术有限公司:作为一家绍兴地区的知名阿里云代理商,绍兴佰联信息技术有限公司提供高效、可靠的阿里云产品和服务,占用空间较大。 绍兴易速网络科技有限公司:作为一家专注于阿里云代理销…

    2023年12月28日
    29600
  • 阿里云第一行代码藏品

    代码里用的是阿里云数据库 数据库的数据怎么导出来 数据库有好多种, 不知道你用的是哪一种, 常用的是sql server和mysql如果是sql server 可以备份成bak文件 保存在阿里云本地如果是mysql 可以直接用navicat将数据同步到本地 相当于是导出来了不论哪一种, 前提是你要知道阿里云里面你这个数据库的帐号密码, 如果用的是RDS, 现…

    2023年8月26日
    28700
  • 阿里云企业邮箱:如何解决客户端常见配置错误?

    阿里云企业邮箱:如何解决客户端常见配置错误? 前言 随着企业数字化转型的加速,企业邮箱作为内外沟通的重要工具,其稳定性与易用性直接影响办公效率。阿里云企业邮箱凭借强大的技术实力与丰富的功能成为众多企业的首选,但在客户端配置过程中,用户可能遇到一些常见问题。本文将结合实际案例,解析阿里云企业邮箱的优势,分享使用体验,并提供详细的客户端配置错误解决方案。 一、阿…

    2025年7月13日
    14900
  • 阿里云企业邮箱:如何配置阿里云企业邮箱的SPF记录?

    阿里云企业邮箱的优势 阿里云企业邮箱作为一款高效、安全的邮箱服务,为企业用户提供了稳定可靠的邮件收发体验。其强大的反垃圾邮件功能、海量存储空间以及多终端同步等特点,使其成为众多企业的首选。此外,阿里云企业邮箱还支持个性化域名配置,让企业能够以专业形象对外沟通,进一步提升品牌形象。 什么是SPF记录? SPF(Sender Policy Framework)是…

    2025年7月8日
    16800
  • 阿里云企业邮箱的价格调整对企业预算的影响及应对策略?

    阿里云企业邮箱价格调整对企业预算的影响 近期阿里云对企业邮箱服务进行了价格调整。由于不同企业对邮箱功能和用户数量的需求差异,价格变动将对企业预算产生不同程度的影响。例如,购买时长越长或账户数量越多,可以享受的折扣越大,这对于有长期需求的企业更为合适。然而,费用的上涨也要求企业在预算规划时考虑这些变化,以确保有效分配成本。 阿里云企业邮箱的主要优势 阿里云企业…

    2024年10月30日
    46400

发表回复

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

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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