阿里云短信代码

阿里云短信代码示例:

引入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年8月18日
    75000
  • 阿里云企业邮箱:如何在阿里云企业邮箱中快速恢复已删除的邮件?

    阿里云企业邮箱:高效与安全的邮件管理专家 在现代企业管理中,高效沟通与数据安全至关重要。阿里云企业邮箱凭借其专业的企业级服务能力,为用户提供了稳定可靠的邮件系统解决方案。本文将重点介绍如何通过阿里云企业邮箱快速恢复误删邮件,并深入解析该产品的核心优势。 一键找回误删邮件 日常工作中难免误删重要邮件,阿里云企业邮箱的「邮件回收站」功能让恢复变得轻而易举。用户只…

    2025年8月16日
    59100
  • 阿里云数据库mysql价格

    阿里云数据库MySQL的价格根据不同的规格和配置有所差异,以下是一些常见的价格参考: 阿里云RDS MySQL:RDS是阿里云的托管式数据库服务,提供了多种规格和配置选项。以一台2核4GB的MySQL实例为例,价格约为1000元/月左右,具体价格还取决于地域、存储空间等因素。 阿里云PolarDB for MySQL:PolarDB是阿里云的云原生数据库服务…

    2023年8月13日
    78100
  • 阿里云计算acp认证有题库吗

    AWS认证考试复杂吗?是不是很难通过?有通过的人吗?需要注意些什么?好多问题怎么办? 您好,刚刚考过了AWS架构师认证不是很难,理解基本概念就可以了,80分钟的考试时间AWS才是真正的云计算,国内的什么各种“云”只是炒作概念,把虚拟主机说成“云”如果真想学云计算,还是AWS,佩服美国人的专业和创新,鄙视国内各种山寨炒作 国内有哪些云计算的培训和认证考试比较靠…

    2023年8月27日
    65800
  • 阿里云web应用防火墙合作商

    请问:阿里云服务商代理有哪些公司? 很稳定的,这么巧我也是找的思 朴科技,不过是买的阿里云的cdn,还不错 . web防火墙 有哪些厂家 挺多的,OEM占据大部分。自主研发的:铱迅,绿盟,启明,安恒,天泰,天存等。飞塔的waf在国内不卖了。waf还是建议用国内的。 imperva web应用防火墙哪家的好 IMPERVA的web应用防火墙还不错的,中国官网地…

    2023年8月29日
    69100

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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