阿里云实现发送短信(Java实例教程)

🍋发送验证码

短信发送是电信运营商提供的服务,需要访问对应的接口,不同运营商提供的接口地址肯定不一样,如果直接访问这些接口就需要判断收信息的手机号属于哪个运营商,关键在于这些接口不对个人开放,还要考虑调用短信服务的费用问题
在这里插入图片描述

因此目前调用短信业务都是使用第三方企业的短信服务,他们与运营商合作,封装了短信接口,调用方法,而且费用相对便宜
在这里插入图片描述

第三方的短信服务有很多,其中阿里云也提供了短信服务

🧣短信服务(推荐)

🐳注册购买

第一步:阿里云首页搜索短信服务

地址:添加链接描述
在这里插入图片描述

在这里插入图片描述

第二步:选择购买的短信服务
在这里插入图片描述

第三步:点击购买,有5条免费使用,测试也会消耗使用次数,用完了在付费购买即可
在这里插入图片描述

第四步:找到自己购买的云服务

在这里插入图片描述

  • 可以看到已购买的服务剩余数量

在这里插入图片描述

🏓代码测试

第一步:参考API,在【API接口】中已经给出了Java代码怎么调用该服务的接口

第二步:参考API,编写发送短信工具类

import com.aliyun.tea.TeaModel;

/***
 * @Title:
 * @ClassName: com.hssmart.common.utils.AliyunSms.java
 * @Description:
 *
 * @Copyright  suihao- Powered By 研发中心
 * @author: suihao
 * @date:  2022-11-01 15:51
 * @version V1.0
 */
public class AliyunSms {
    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId 
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

}

accessKeyId 以及accessKeySecret查找的方式“:
第一步点击头像打开accessKey管理
在这里插入图片描述
第二部进行查看所需要的accessKeyId 以及accessKeySecret
在这里插入图片描述

🖐Java组件封装

🥝发送实例
package com.suihao.autoconfig.properties;

public static void main(String[] args) {
    com.aliyun.dysmsapi20170525.Client client = AliyunSms.createClient("accessKeyId", "accessKeySecret");
    com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
            .setSignName("签名名称")
            .setTemplateCode("模板号码")
            .setPhoneNumbers("测试手机号")
            .setTemplateParam("{\"code\":\"6666\"}");
    com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
    com.aliyun.dysmsapi20170525.models.SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
    com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
  }

1.SignName代表的签名名称
在这里插入图片描述
2.TemplateCode代表的模板code
在这里插入图片描述

🥝pom依赖
 <!--阿里云-->
    <developers>
        <developer>
            <id>aliyundeveloper</id>
            <name>Aliyun SDK</name>
            <email>aliyunsdk@aliyun.com</email>
        </developer>
    </developers>
    <distributionManagement>
        <snapshotRepository>
            <id>sonatype-nexus-snapshots</id>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>sonatype-nexus-staging</id>
            <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>
    <scm>
        <connection></connection>
        <developerConnection></developerConnection>
        <url></url>
    </scm>
    <dependencies>
        <!--阿里云-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dysmsapi20170525</artifactId>
            <version>2.0.22</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>tea-openapi</artifactId>
            <version>0.2.6</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>tea-console</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>tea-util</artifactId>
            <version>0.2.14</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>tea</artifactId>
            <version>1.1.14</version>
        </dependency>
        <!---->
  </dependencies>
  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>1.6.3</version>
                <extensions>true</extensions>
                <configuration>
                    <serverId>sonatype-nexus-staging</serverId>
                    <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>
        </plugins>
    </build>
🌈Spring.factories(略)

🍎麻烦给博主点个关注+收藏+点赞!

  • 14
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
根据引用\[1\]中的阿里云服务官方文档,您可以通过以下步骤来创建一个阿里云发送的工具类: 1. 开通阿里云账号并登录,然后申请一个子权限账户,并授予该账户服务管理的权限。 2. 在服务控制台中申请签名和模板,具体的规则可以参考审核标准。 3. 根据接收对象的不同,选择相应的签名和模板。 4. 调用阿里云服务的接口来发送。 在编写工具类时,您可以使用引用\[2\]中提供的阿里云依赖,即aliyun-java-sdk-core和aliyun-java-sdk-dysmsapi。您可以在您的项目中添加这两个依赖,并按照官方文档提供的示例代码来调用阿里云服务的接口。 如果您需要同时向多个不同的手机号码发送不同签名的,可以参考引用\[3\]中的文档,该文档提供了相关的参考息。 请注意,以上是一个简要的概述,具体的实现细节和代码编写需要根据您的具体需求和项目环境进行调整。建议您参考阿里云服务的官方文档和示例代码来完成您的工具类的编写。 #### 引用[.reference_title] - *1* *3* [阿里云服务工具类](https://blog.csdn.net/qq_40147276/article/details/93724745)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【阿里云】服务 无认证 测试版本 附带发送工具类](https://blog.csdn.net/SPX113/article/details/125862420)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江東-H

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值