阿里云短信代码

阿里云短信代码示例:

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

相关推荐

  • 阿里云媒体与融合通信事业部

    阿里云媒体与融合通信事业部是阿里云的一个业务部门,专注于媒体和融合通信领域的技术研发、产品开发和市场推广。该事业部的目标是通过云计算、大数据、人工智能等先进技术,提供全方位的媒体解决方案和融合通信服务,帮助客户实现数字化转型和业务创新。 阿里云媒体与融合通信事业部的主要业务包括视频云、音频云、直播云、CDN加速、智能客服、物联网通信等多个领域。视频云提供高清…

    2023年8月6日
    6600
  • 保定阿里云代理商:阿里云图库

    保定阿里云代理商“阿里云图库”是一家为保定地区提供阿里云产品和服务的代理商。阿里云图库代理商致力于为企业和个人用户提供完善的阿里云解决方案,包括云服务器、云数据库、云存储、云安全等多个领域的产品和服务。 作为阿里云官方认证的代理商,阿里云图库具备丰富的产品知识和技术实力,可以为客户提供个性化的解决方案和专业的技术支持。无论是中小型企业还是大型企业,阿里云图库…

    2023年12月26日
    4500
  • 阿里云盘好的资源

    哪个网盘的资源会比较多啊? 资源最多肯定是百度网盘,没有之一 有什么好用的免费网盘 这对于免费网盘也算是很快的了,天 翼 云里面的资源广场里面的资源丰富度,而且还要做任务才有2、3T就不错了、3T容量;然后就是网盘里面的资源很丰富,首先它初始容量有15G,呵呵,我家6M宽带,当然还是要看你家里的网速给不给力,可以保证你重要文件的安全,我觉得比较好用的免费网盘…

    2023年8月29日
    4300
  • 广州阿里云代理商:阿里防火墙解决方案

    阿里防火墙是阿里云提供的一种强大的网络安全防御系统,可以帮助企业实现安全保障、减少攻击、提高系统稳定性,防止黑客攻击和恶意软件的侵入。 阿里防火墙主要包括以下特点: 智能识别:阿里防火墙能够智能地识别和过滤恶意流量和攻击,确保业务的安全性和可用性。 全方位防御:阿里防火墙提供了多种防御方式,包括DDoS攻击防护、Web应用防护、SQL注入攻击、反爬虫等,从多…

    2023年12月14日
    7700
  • 濮阳阿里云企业邮箱代理商:阿里云邮箱在苹果手机怎么设置

    濮阳阿里云企业邮箱代理商:阿里云邮箱在苹果手机怎么设置 优势分析 阿里云企业邮箱作为阿里云旗下的企业级邮箱服务,具有诸多优势,例如: 稳定可靠:基于阿里云强大的云计算技术,保障邮箱服务的稳定性和可靠性。 安全加密:采用SSL加密技术,确保邮件传输的安全性,防止信息泄露。 大容量存储:提供大容量的邮箱存储空间,满足企业日常邮件收发需求。 多端同步:支持多种设备…

    2024年2月21日
    5500

发表回复

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

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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