Bitzsoft.Integrations.Sms 1.0.0-alpha.3

This is a prerelease version of Bitzsoft.Integrations.Sms.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Bitzsoft.Integrations.Sms --version 1.0.0-alpha.3
                    
NuGet\Install-Package Bitzsoft.Integrations.Sms -Version 1.0.0-alpha.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bitzsoft.Integrations.Sms" Version="1.0.0-alpha.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bitzsoft.Integrations.Sms" Version="1.0.0-alpha.3" />
                    
Directory.Packages.props
<PackageReference Include="Bitzsoft.Integrations.Sms" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bitzsoft.Integrations.Sms --version 1.0.0-alpha.3
                    
#r "nuget: Bitzsoft.Integrations.Sms, 1.0.0-alpha.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bitzsoft.Integrations.Sms@1.0.0-alpha.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bitzsoft.Integrations.Sms&version=1.0.0-alpha.3&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Bitzsoft.Integrations.Sms&version=1.0.0-alpha.3&prerelease
                    
Install as a Cake Tool

Bitzsoft.Integrations.Sms

短信双通道集成 -- 阿里云短信 + 腾讯云短信统一抽象,一套接口切换两大云厂商。

功能特性

  • 统一 ISMS 接口,屏蔽阿里云与腾讯云 SDK 差异
  • 支持 SendAsync 异步发送,返回标准化 SMSResult
  • 阿里云短信:基于 OpenPlatform POP 签名调用
  • 腾讯云短信:基于腾讯云 API 3.0 签名调用
  • Options 强类型配置,支持 IConfigurationSection 绑定
  • 独立 DI 注册,可按需引入单通道或双通道

安装

dotnet add package Bitzsoft.Integrations.Sms

或直接在项目文件中引用:

<PackageReference Include="Bitzsoft.Integrations.Sms" Version="*" />

配置

appsettings.json 中添加对应通道配置:

{
  "AliyunSms": {
    "AccessKeyId": "your-access-key-id",
    "AccessKeySecret": "your-access-key-secret",
    "RegionId": "cn-hangzhou",
    "Domain": "dysmsapi.aliyuncs.com",
    "Version": "2017-05-25"
  },
  "TencentSms": {
    "SecretId": "your-secret-id",
    "SecretKey": "your-secret-key",
    "AppId": "1400xxxxxx",
    "Region": "ap-guangzhou",
    "CountryCode": "+86"
  }
}

注册服务

阿里云短信通道

using Bitzsoft.Integrations.Sms.Aliyun;

// 方式一:通过 IConfigurationSection 绑定
builder.Services.AddAliyunSms(builder.Configuration.GetSection("AliyunSms"));

// 方式二:通过委托手动配置
builder.Services.AddAliyunSms(options =>
{
    options.AccessKeyId = "your-access-key-id";
    options.AccessKeySecret = "your-access-key-secret";
    options.RegionId = "cn-hangzhou";
    options.Domain = "dysmsapi.aliyuncs.com";
    options.Version = "2017-05-25";
});

腾讯云短信通道

using Bitzsoft.Integrations.Sms.Tencent;

// 方式一:通过 IConfigurationSection 绑定
builder.Services.AddTencentSms(builder.Configuration.GetSection("TencentSms"));

// 方式二:通过委托手动配置
builder.Services.AddTencentSms(options =>
{
    options.SecretId = "your-secret-id";
    options.SecretKey = "your-secret-key";
    options.AppId = "1400xxxxxx";
    options.Region = "ap-guangzhou";
    options.CountryCode = "+86";
});

使用示例

以下示例展示在业务服务中通过阿里云通道发送验证码短信:

using System.Text.Json;
using Bitzsoft.Integrations.Sms;

/// <summary>
/// 用户注册服务
/// </summary>
public class RegistrationService
{
    private readonly ISMS _sms;

    /// <summary>
    /// 初始化注册服务实例
    /// </summary>
    /// <param name="sms">短信发送服务(由 DI 注入具体通道实现)</param>
    public RegistrationService(ISMS sms)
    {
        _sms = sms;
    }

    /// <summary>
    /// 发送手机验证码
    /// </summary>
    /// <param name="phoneNumber">目标手机号</param>
    /// <param name="verificationCode">6 位数字验证码</param>
    /// <param name="cancellationToken">取消令牌</param>
    public async Task<bool> SendVerificationCodeAsync(
        string phoneNumber,
        string verificationCode,
        CancellationToken cancellationToken = default)
    {
        var templateParams = JsonSerializer.Serialize(new { code = verificationCode });

        SMSResult result = await _sms.SendAsync(
            phoneNumber: phoneNumber,
            signName: "我的应用",
            templateCode: "SMS_123456789",
            templateParams: templateParams,
            cancellationToken: cancellationToken);

        if (!result.Success)
        {
            // 记录失败日志,包含错误码与错误消息
            Console.WriteLine(
                "短信发送失败: ErrorCode={0}, ErrorMessage={1}",
                result.ErrorCode,
                result.ErrorMessage);
            return false;
        }

        // 可将 result.BizId 存储用于后续状态查询
        Console.WriteLine("短信发送成功, BizId={0}", result.BizId);
        return true;
    }
}

依赖

  • Microsoft.Extensions.Logging.Abstractions -- 日志抽象
  • Microsoft.Extensions.Configuration.Abstractions -- 配置抽象
  • Microsoft.Extensions.Options -- Options 配置模式
  • Microsoft.Extensions.Options.ConfigurationExtensions -- 配置节点绑定
  • aliyun-net-sdk-core -- 阿里云 OpenAPI SDK
  • Aliyun.OSS.SDK.NetCore -- 阿里云 SDK 基础依赖
  • TencentCloudSDK -- 腾讯云短信 SDK

相关包

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Bitzsoft.Integrations.Sms:

Package Downloads
Bitzsoft.Integrations.All

Bitzsoft 第三方集成聚合包 — 包含全部 Integration 模块

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-alpha.8 37 7/1/2026
1.0.0-alpha.7 63 6/16/2026
1.0.0-alpha.6 69 6/16/2026
1.0.0-alpha.5 60 6/14/2026
1.0.0-alpha.3 59 6/7/2026
1.0.0-alpha.2 60 5/29/2026
1.0.0-alpha.1 59 5/28/2026