Implementing Magicodes.Sms for Alibaba Cloud SMS in .NET Applications

Overview

Package Name Purpose
Magicodes.Sms.Aliyun Adaptor for Alibaba Cloud SMS Provider
Magicodes.Sms.Core Base interfaces and core logic
Magicodes.Sms.Aliyun.Abp Dependency Injection registration for ABP Modules

When utilizing the ABP architecture, the integration follows a modular dependency pattern.

1. Install Dependencies

Add the specific ABP provider package to your domain project or infrastructure project referencing the solution:

dotnet add package Magicodes.Sms.Aliyun.Abp

2. Declare Module Dependency

In your application's entry module (e.g., MyAppModule.cs), declare the dependency on the SMS provider module:

[DependsOn(typeof(AliyunSmsModule))]
public class MyAppModule : AbpModule { }

3. Application Configuraton

Configure credentials and template identifiers with in the appsettings.json file. Ensure the section matches the expected hierarchy:

{
  "AliyunSms": {
    "AccessKeyId": "your_access_key_id",
    "SecretKey": "your_secret_key",
    "SignName": "YourCompanyBrand",
    "TemplateCode": "SMS_123456789"
  }
}

4. Runtime Usage

Inject the sender interface into your controller or business logic service and invoke the asynchronous send method:

using Microsoft.AspNetCore.Mvc;
using Magicodes.Sms;

public class NotificationController : Controller
{
    private readonly ISmsService _smsService;

    public NotificationController(ISmsService smsService)
    {
        _smsService = smsService;
    }

    [HttpPost("send-verification")]
    public async Task<iactionresult> SendCode(string targetPhone, string token)
    {
        var result = await _smsService.SendCodeAsync(targetPhone, token);
        // Handle response status here
        return Ok();
    }
}</iactionresult>

Standalone .NET Integration

For console applications or services outside the ABP scope, direct initialization of the builder pattern is required.

Initialization

Construct the configuration using the fluent builder API, defining logging hooks and settings retrieval mechanisms:

var configuration = AliyunSmsBuilder.Create()
    .WithLogging((tag, msg) => Console.WriteLine($"[{tag}] {msg}"))
    .WithSettingsLoader(() => 
    {
        // Load configuration from environment variables or config files
        return GetAppSettings();
    })
    .Build();

Sending Messages

Instentiate the service class and execute the verification code dispatch logic:

[Fact]
public async Task Test_Albert_Send_Operation()
{
    // Retrieve settings previously configured
    var factory = AliyunSmsBuilder.Create().Build();
    
    // Perform the action
    var outcome = await factory.SendVerificationCode(
        "13800138000", 
        "998877"
    );
    
    Assert.True(outcome.IsSuccess);
}

Tags: Magicodes.Sms AbpFramework AlibabaCloud DotNetCore SMSIntegration

Posted on Tue, 15 Sep 2026 16:13:45 +0000 by insane