泉州阿里云代理商:阿里通信短信demo

阿里通信短信服务是阿里云提供的一种短信发送解决方案,用于短信验证码、营销短信等业务场景。以下是用于发送短信的阿里通信短信demo示例代码:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sms.model.v20170525.*;
import java.util.Random;

public class AliyunSmsDemo {
    public static void main(String[] args) {
        // AK、SK等配置信息
        String accessKeyId = "your-access-key-id";
        String accessKeySecret = "your-access-key-secret";

        // 短信签名
        String signName = "your-sign-name";
        
        // 短信模板Code
        String templateCode = "your-template-code";
        
        // 短信接收号码
        String phoneNumber = "your-phone-number";
        
        // 生成6位随机验证码
        String code = String.valueOf(new Random().nextInt(999999));


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

    /**
     * 发送短信
     *
     * @param accessKeyId     阿里云AccessKeyId
     * @param accessKeySecret 阿里云AccessKeySecret
     * @param signName        短信签名
     * @param templateCode    短信模板Code
     * @param phoneNumber     短信接收号码
     * @param code            验证码
     * @return SendSmsResponse
     * @throws ClientException
     */
    public static SendSmsResponse sendSms(String accessKeyId, String accessKeySecret, String signName, String templateCode, String phoneNumber, String code)
            throws ClientException {
        // 初始化DefaultAcsClient
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultSmsClient client = new DefaultSmsClient(profile);

        // 创建发送短信的请求对象
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phoneNumber); // 设置短信接收号码
        request.setSignName(signName); // 设置短信签名
        request.setTemplateCode(templateCode); // 设置短信模板Code
        request.setTemplateParam("{"code": " + code + "}"); // 设置短信模板参数

        // 发送短信
        SendSmsResponse response = client.getAcsResponse(request);

        return response;
    }
}

使用时,需要替换代码中的以下配置信息:

  • accessKeyIdaccessKeySecret:阿里云的AccessKeyId和AccessKeySecret,在阿里云控制台的RAM访问控制中可以获取。
  • signName:短信签名,需要在阿里云短信服务中申请并审核通过。
  • templateCode:短信模板Code,需要在阿里云短信服务中申请并审核通过。
  • phoneNumber:短信接收号码,可以是手机号码或者阿里云短信服务中申请的测试号码。

运行以上代码,就可以发送短信了。请注意,阿里云短信服务需要付费使用,具体费用请参考阿里云官方文档。

请参考以下阿里通信短信demo示例:

泉州阿里云代理商:阿里通信短信demo
import java.util.HashMap;
import java.util.Map;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teaopenapi.modules.AcsError;
import com.aliyun.teaopenapi.modules.ApiException;
import com.aliyun.teaopenapi.modules.ApiRequest;
import com.aliyun.teaopenapi.modules.ApiResponse;
import com.aliyun.teaopenapi.modules.DefaultAcsClient;

public class AliyunSmsDemo {
    private static final String accessKeyId = "<你的AccessKeyId>";
    private static final String accessKeySecret = "<你的AccessKeySecret>";
    private static final String signName = "<短信签名名称>";
    private static final String templateCode = "<短信模板CODE>";

    public static void main(String[] args) {
        Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(config);

        SendSmsRequest request = new SendSmsRequest()
                .setPhoneNumbers("<收信人手机号>")
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam("{"code":"123456"}"); // 替换为实际的短信模板参数

        try {
            SendSmsResponse response = client.sendRequest(request);
            if (response.getCode().equals("OK")) {
                System.out.println("短信发送成功!");
            } else {
                System.out.println("短信发送失败:" + response.getCode() + "-" + response.getMessage());
            }
        } catch (ApiException e) {
            AcsError ae = (AcsError) e.getErrModel();
            System.out.println("短信发送失败:" + ae.getErrorCode() + "-" + ae.getErrorMessage());
        }
    }
}

请将<你的AccessKeyId><你的AccessKeySecret><短信签名名称><短信模板CODE><收信人手机号>替换为实际的参数。

运行该demo即可发送短信。

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

(0)
luotuoemo的头像luotuoemo
上一篇 2023年12月18日 07:08
下一篇 2023年12月18日 07:20

相关推荐

  • 阿里云服务器租赁费

    阿里云服务器的租赁费用根据不同的配置和使用情况而变化。一般来说,阿里云服务器的租赁费用可以分为按需计费和预付费两种方式。 按需计费是根据服务器的实际使用时长和资源消耗进行计费,费用会随着实际使用情况发生变化。 预付费是提前支付一定时长的费用,根据不同的配置和时长进行收费。一般来说,预付费费用会比按需计费低一些。 具体的租赁费用需要根据您选择的服务器配置和使用…

    2023年8月26日
    16100
  • 菏泽阿里云代理商:api619

    阿里云代理商:api619 引言 阿里云是中国领先的云计算服务提供商,其全面的产品线和强大的技术实力使其成为企业数字化转型的首选。本文将重点介绍阿里云代理商api619的优势及其在解决企业问题方面的能力。 1. 强大的云计算基础设施 阿里云作为一家全球级的云计算服务提供商,拥有庞大而稳定的基础设施。无论是计算能力还是存储容量,阿里云都具备强大的承载能力,可以…

    2024年1月23日
    16500
  • 潍坊阿里云代理商:阿里云 yum切换

    潍坊阿里云代理商提供阿里云服务器上使用yum命令进行软件包管理时,需要将yum源切换到阿里云的镜像源,以提高软件包下载速度和稳定性。以下是如何在阿里云服务器上进行yum源切换的步骤: 1.备份原有yum源配置文件在终端上执行以下命令备份原有的yum源配置文件: sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yu…

    2024年2月23日
    18900
  • 扬州阿里云代理商:ajax直接加载数据库数据库数据

    在扬州地区,阿里云代理商可以为您提供支持,并帮助您实现将数据库数据直接加载到页面中的功能。对于使用AJAX技术来实现这一目标,您可以按照下面的步骤进行操作: 在您的页面中引入jQuery库,这是常用的用于简化AJAX操作的JavaScript库。可以通过在页面的头部引入以下代码来加载jQuery库: <script src="https://…

    2024年2月8日
    16800
  • 济南阿里云代理商:autocommit

    autocommit是MySQL数据库的一个参数,用于控制事务提交的行为。当autocommit设置为1时,表示自动提交事务;当设置为0时,表示需要手动提交事务。 在MySQL中,默认情况下,autocommit的值为1,即自动提交事务。 在使用autocommit参数时,可以通过以下方式进行设置: 在MySQL命令行界面中,使用以下命令进行设置: SET …

    2024年1月11日
    14500

发表回复

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

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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