Luosimao.Sms
1.0.1
dotnet add package Luosimao.Sms --version 1.0.1
NuGet\Install-Package Luosimao.Sms -Version 1.0.1
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="Luosimao.Sms" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Luosimao.Sms" Version="1.0.1" />
<PackageReference Include="Luosimao.Sms" />
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 Luosimao.Sms --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Luosimao.Sms, 1.0.1"
#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 Luosimao.Sms@1.0.1
#: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=Luosimao.Sms&version=1.0.1
#tool nuget:?package=Luosimao.Sms&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Luosimao.Sms
螺丝帽(Luosimao)SMS 服务的 C# SDK,支持单发、批量、定时发送及余额查询。
功能特性
- ✅ 单条短信发送(验证码、触发类消息)
- ✅ 批量短信发送(通知、提醒类消息)
- ✅ 定时短信发送
- ✅ 账户余额查询
- ✅ Webhook 状态回执模型
- ✅ 完整的错误码和异常处理
- ✅ 依赖注入(DI)支持
- ✅
IHttpClientFactory集成
系统要求
- .NET Standard 2.0 及以上
- .NET Framework 4.6.1+
- .NET Core 2.0+
- .NET 5/6/7/8+
安装方式
NuGet 包管理器
dotnet add package Luosimao.Sms
Package Manager Console
Install-Package Luosimao.Sms
快速开始
1. 配置服务(ASP.NET Core)
在 Program.cs 中注册服务:
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
// 注册螺丝帽短信服务
builder.Services.AddLuosimaoSms(options =>
{
options.ApiKey = "your-api-key-here"; // 不含 "key-" 前缀
options.TimeoutSeconds = 30;
});
var app = builder.Build();
2. 发送单条短信
using Luosimao.Sms;
using Luosimao.Sms.Exceptions;
public class SmsService
{
private readonly ILuosimaoSmsClient _smsClient;
public SmsService(ILuosimaoSmsClient smsClient)
{
_smsClient = smsClient;
}
public async Task SendVerificationCodeAsync(string mobile, string code)
{
try
{
// 短信内容末尾必须包含签名
var message = $"您的验证码是:{code}【签名】";
var result = await _smsClient.SendAsync(mobile, message);
Console.WriteLine($"发送成功,批次号:{result.BatchId}");
}
catch (LuosimaoSmsException ex) when (ex.ErrorCode == -31)
{
Console.WriteLine($"发送失败,敏感词:{ex.HitWord}");
}
catch (LuosimaoSmsException ex)
{
Console.WriteLine($"API 错误:{ex.ErrorCode} - {ex.ErrorMessage}");
}
}
}
3. 批量发送短信
var mobiles = new[] { "13800138000", "13800138001", "13800138002" };
var message = "您的订单已发货,请注意查收【签名】";
// 立即发送
var result = await _smsClient.SendBatchAsync(mobiles, message);
// 定时发送
var scheduledTime = DateTime.Now.AddHours(1);
var scheduledResult = await _smsClient.SendBatchAsync(mobiles, message, scheduledTime);
4. 查询账户余额
var status = await _smsClient.GetAccountStatusAsync();
Console.WriteLine($"短信余额:{status.Deposit} 条");
5. 处理 Webhook 状态回执
在您的 Web 服务中接收状态推送:
[HttpPost("webhook/sms-status")]
public IActionResult ReceiveStatusCallback([FromForm] DeliveryReceipt receipt)
{
switch (receipt.Status)
{
case "DELIVRD":
Console.WriteLine($"手机号 {receipt.Mobile} 接收成功");
break;
case "UNDELIV":
Console.WriteLine($"手机号 {receipt.Mobile} 接收失败");
break;
case "REJECTED":
Console.WriteLine($"手机号 {receipt.Mobile} 被退回,原因:{receipt.Memo}");
break;
}
return Ok();
}
错误处理
SDK 在以下情况下会抛出异常:
| 异常类型 | 触发条件 |
|---|---|
ArgumentException |
参数校验失败(手机号为空、号码超限等) |
LuosimaoSmsException |
API 返回 error != 0 |
InvalidOperationException |
JSON 解析失败或响应为空 |
HttpRequestException |
HTTP 请求失败(非 2xx 状态码) |
LuosimaoSmsException 属性
public class LuosimaoSmsException : Exception
{
public int ErrorCode { get; } // 错误码
public string ErrorMessage { get; } // 错误描述
public string HitWord { get; } // 敏感词(仅 error=-31 时有值)
}
完整错误码表
| 错误码 | 描述 | 适用接口 |
|---|---|---|
| 0 | 成功 | 全部 |
| -10 | 验证信息失败 | 全部 |
| -11 | 用户接口被禁用 | 单发 |
| -12 | 余额冻结 | 单发 |
| -20 | 短信余额不足 | 单发、批量 |
| -30 | 短信内容为空 | 单发、批量 |
| -31 | 短信内容含敏感词 | 单发、批量 |
| -32 | 缺少签名信息 | 单发、批量 |
| -33 | 短信超长(>300字) | 单发 |
| -34 | 签名不可用 | 单发、批量 |
| -35 | 测试签名次数达上限 | 单发 |
| -40 | 错误的手机号 | 单发、批量 |
| -41 | 号码在黑名单中 | 单发 |
| -42 | 验证码发送频率过快 | 单发 |
| -43 | 号码数量过多 | 批量 |
| -50 | IP 不在白名单内 | 全部 |
| -60 | 定时时间为过去时间 | 批量 |
项目结构
Luosimao.Sms/
├── Luosimao.Sms.csproj
├── LuosimaoSmsClient.cs # 客户端实现
├── ILuosimaoSmsClient.cs # 客户端接口
├── LuosimaoSmsOptions.cs # 配置选项
├── Models/
│ ├── SmsResponse.cs # 单发响应
│ ├── BatchSmsResponse.cs # 批量响应
│ ├── AccountStatus.cs # 余额查询响应
│ └── DeliveryReceipt.cs # Webhook 回执
├── Exceptions/
│ └── LuosimaoSmsException.cs # 自定义异常
└── Extensions/
└── ServiceCollectionExtensions.cs # DI 扩展
Luosimao.Sms.Tests/
├── Luosimao.Sms.Tests.csproj
└── LuosimaoSmsClientTests.cs # 单元测试
运行测试
dotnet test Luosimao.Sms.Tests/Luosimao.Sms.Tests.csproj
构建与打包
# 构建 Release 版本
dotnet build Luosimao.Sms/Luosimao.Sms.csproj -c Release
# 打包 NuGet 包
dotnet pack Luosimao.Sms/Luosimao.Sms.csproj -c Release
贡献代码
欢迎提交 Issue 和 Pull Request!
- Fork 本仓库
- 创建特性分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 创建 Pull Request
许可证
本项目基于 MIT 许可证开源,详见 LICENSE 文件。
致谢
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. 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 was computed. 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 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.Extensions.Http (>= 3.1.32)
- System.Text.Json (>= 8.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.