泉州阿里云代理商:阿里通信短信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

相关推荐

  • python机器学习云平台

    有很多Python机器学习云平台可供选择,例如: Google Cloud AI Platform:提供了完整的机器学习工作流程,包括数据准备、模型训练、部署和服务。 Microsoft Azure Machine Learning:结合了云计算和机器学习的功能,支持在云端快速构建、训练和部署模型。 Amazon SageMaker:适用于构建、训练和部署机…

    2023年8月17日
    12500
  • 什么是 阿里云 物联网服务

    物联网云服务是什么? 比较成功点的应该还是氦氪云了,它的云服务是真的的很贴心。其打造的智能家居生活方式,令用户们真的是惊喜万分。 什么是物联网啊??? 物联网(The Internet of things):通过射频识别(RFID)、红外感应器、全球定位系统、激光扫描器等信息传感设备,按约定的协议,把任何物品与互联网连接起来,进行信息交换和通讯,以实现智能化…

    2023年8月27日
    13100
  • 阿里云企业邮箱迁移后的邮件阅读界面显示效果可优化吗?

    阿里云企业邮箱迁移后的邮件阅读界面优化探讨 随着数字化办公的不断推进,企业邮箱作为日常沟通的核心工具,其界面的使用体验直接影响到工作效率和团队协作。阿里云企业邮箱以其稳定性和安全性而受到广泛欢迎,但在进行邮箱迁移后,用户常常会发现邮件阅读界面存在一定的优化空间。本文将探讨阿里云企业邮箱迁移后的邮件阅读界面显示效果的优化,并结合阿里云企业邮箱及其代理商的优势,…

    2024年10月31日
    8300
  • 张掖阿里云企业邮箱代理商:阿里邮箱企业版服务器地址怎么改

    张掖阿里云企业邮箱代理商:阿里邮箱企业版服务器地址怎么改 作为阿里云企业邮箱代理商,我们将向您介绍如何修改阿里邮箱企业版的服务器地址。在这篇文章中,我们将简单明了地解释这个过程,并强调阿里云企业邮箱和阿里云企业邮箱代理商的优势。 阿里云企业邮箱及其优势 阿里云企业邮箱是一种基于云计算的企业级邮件解决方案。它为企业提供了强大的电子邮件功能和稳定可靠的服务器,可…

    2024年2月4日
    14700
  • 济源阿里云企业邮箱代理商:阿里邮箱咋样

    阿里云企业邮箱:优势和好用之处 1. 阿里云企业邮箱介绍 阿里云企业邮箱是由阿里巴巴集团旗下的阿里云提供的一款高效、安全、稳定的企业邮箱服务。它基于阿里云强大的技术支持,为企业用户提供全面的邮件管理解决方案。 2. 阿里云企业邮箱的优势 2.1 提供强大的安全保障 阿里云企业邮箱采用先进的反垃圾、反病毒技术,有效过滤垃圾邮件和恶意软件,保障企业邮箱的安全。 …

    2024年1月27日
    13500

发表回复

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

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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