阿里云短信代码

阿里云短信代码示例:

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

相关推荐

  • 太原阿里云代理商:阿里云应用商店apk

    阿里云应用商店是阿里云推出的面向移动应用开发者与企业的应用发布与分发平台,提供了丰富的应用资源和商业化服务。作为太原的阿里云代理商,可以通过阿里云的官方渠道下载并安装阿里云应用商店的apk文件。 具体操作步骤如下: 打开手机的浏览器,输入阿里云的官方网址:www.aliyun.com。 进入阿里云官网后,点击页面右上角的登录按钮,使用您的阿里云账号登录。 登…

    2024年1月28日
    66000
  • 北流阿里云企业邮箱代理商:qq里面怎么找qq邮箱地址

    北流阿里云企业邮箱代理商:qq邮箱地址的查找及阿里云企业邮箱优势 一、在QQ中查找QQ邮箱地址 QQ是中国最大的即时通信软件之一,其中也提供了QQ邮箱服务。要在QQ中查找QQ邮箱地址,可以按照以下步骤进行: 打开QQ软件并登录账号。 点击左上角的“联系人”图标。 在联系人列表中找到目标联系人。 右键点击该联系人,并选择“查看资料”选项。 在弹出的资料窗口中,…

    2024年1月16日
    78600
  • 香港阿里云代理商:安装证书到受信任区

    为了在香港阿里云服务器上安装证书到受信任区,您可以按照以下步骤操作: 登录到您的阿里云控制台。 导航到SSL证书管理页面。 点击“购买证书”按钮,购买您的SSL证书。根据您的需求选择适当的SSL证书类型。 确认您的域名和服务器信息,并按照阿里云提供的验证步骤验证您的域名所有权。 在证书管理页面,找到您购买的证书,并点击“下载”按钮下载证书文件。 登录到您的阿…

    2024年2月6日
    66600
  • 贵阳阿里云代理商:asp电商网站

    贵阳阿里云代理商是指在贵阳地区代理阿里云产品和服务的公司或个人。阿里云是阿里巴巴集团旗下的云计算平台,提供各种云计算产品和服务,包括云服务器、云数据库、云存储、云安全等。 ASP(Active Server Pages)是一种由微软公司开发的服务器端脚本技术,用于创建和运行动态交互的网站。ASP电商网站是指使用ASP技术开发的电子商务平台,可以实现商品展示、…

    2024年2月16日
    65800
  • 杭州阿里云数据中心是干嘛的

    马云为什么选择在千岛湖设立数据中心 中国的数据中心PUE指数普遍较高,1.5以上马云的数据中心为啥建在千岛湖呢?因为千岛湖有水啊有水能干嘛呢?因为数据中心是耗水大户啊阿里巴巴集团正在利用中国东部的千岛湖的自然水体为他们的位于杭州数据中心进行冷却。你看多省电多省电~这一省电~PUE不就下来了吗~ 阿里云的大数据平台是干嘛的?怎么用?有什么价值? 搜一下:阿里云…

    2023年8月28日
    69900

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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