Filter by title

SMS Sending

The ABP provides an abstraction to sending SMS. Having such an abstraction has some benefits;

  • You can then easily change your SMS sender without changing your application code.
  • If you want to create reusable application modules, you don't need to make assumption about how the SMS are sent.

Installation

It is suggested to use the ABP CLI to install this package.

Using the ABP CLI

Open a command line window in the folder of the project (.csproj file) and type the following command:

abp add-package Volo.Abp.Sms

If you haven't done it yet, you first need to install the ABP CLI. For other installation options, see the package description page.

Manual Installation

If you want to manually install;

  1. Add the Volo.Abp.Sms NuGet package to your project:
dotnet add package Volo.Abp.Sms
  1. Add the AbpSmsModule to the dependency list of your module:
[DependsOn(
    //...other dependencies
    typeof(AbpSmsModule) //Add the new module dependency
    )]
public class YourModule : AbpModule
{
}

Sending SMS

Inject the ISmsSender into any service and use the SendAsync method to send a SMS.

Example:

using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Sms;

namespace MyProject
{
    public class MyService : ITransientDependency
    {
        private readonly ISmsSender _smsSender;

        public MyService(ISmsSender smsSender)
        {
            _smsSender = smsSender;
        }

        public async Task DoItAsync()
        {
            await _smsSender.SendAsync(
                "+012345678901",        // target phone number
                "This is test sms..."   // message text
            );
        }
    }
}

The given SendAsync method in the example is an extension method to send an SMS with primitive parameters. In addition, you can pass an SmsMessage object which has the following properties:

  • PhoneNumber (string): Target phone number
  • Text (string): Message text
  • Properties (IDictionary<string, object>): Key-value pairs to pass custom arguments

NullSmsSender

NullSmsSender is the default implementation of ISmsSender. It writes SMS content to the standard logger, rather than actually sending the SMS.

This class can be useful especially in development time where you generally don't want to send real SMS. To send real SMS, install one of the pre-built providers below or implement ISmsSender in your application code.

Implementing the ISmsSender

You can easily create your SMS sending implementation by creating a class that implements the ISmsSender interface, as shown below:

using System.Threading.Tasks;
using Volo.Abp.Sms;
using Volo.Abp.DependencyInjection;

namespace AbpDemo
{
    public class MyCustomSmsSender : ISmsSender, ITransientDependency
    {
        public Task SendAsync(SmsMessage smsMessage)
        {
            // Send sms
            return Task.CompletedTask;
        }
    }
}

Pre-Built Providers

Adding a provider module registers its sender as the ISmsSender implementation in place of the default NullSmsSender.

Aliyun

Install the Aliyun provider package:

abp add-package Volo.Abp.Sms.Aliyun

For manual installation, add the Volo.Abp.Sms.Aliyun package and declare a dependency on AbpSmsAliyunModule.

Configure the provider in the AbpAliyunSms section:

{
  "AbpAliyunSms": {
    "AccessKeyId": "your-access-key-id",
    "AccessKeySecret": "your-access-key-secret",
    "EndPoint": "your-endpoint"
  }
}

Aliyun sends template-based messages. Set SmsMessage.Text to the template parameter JSON and use the SignName and TemplateCode properties:

var message = new SmsMessage(
    "+012345678901",
    "{\"code\":\"123456\"}"
);

message.Properties["SignName"] = "MySign";
message.Properties["TemplateCode"] = "SMS_123456789";

await _smsSender.SendAsync(message);

Tencent Cloud

Install the Tencent Cloud provider package:

abp add-package Volo.Abp.Sms.TencentCloud

For manual installation, add the Volo.Abp.Sms.TencentCloud package and declare a dependency on AbpSmsTencentCloudModule.

Configure the provider in the AbpTencentCloudSms section:

{
  "AbpTencentCloudSms": {
    "SmsSdkAppId": "your-sdk-app-id",
    "SecretId": "your-secret-id",
    "SecretKey": "your-secret-key",
    "Endpoint": "sms.tencentcloudapi.com",
    "Region": "ap-guangzhou"
  }
}

Endpoint defaults to sms.tencentcloudapi.com and Region defaults to ap-guangzhou.

Set the sign and template identifiers through TencentCloudSmsProperties. The provider splits SmsMessage.Text by commas and sends the resulting values as template parameters:

var message = new SmsMessage(
    "+012345678901",
    "123456,5"
);

message.Properties[TencentCloudSmsProperties.SignName] = "MySign";
message.Properties[TencentCloudSmsProperties.TemplateId] = "123456";

await _smsSender.SendAsync(message);

More

ABP provides Twilio integration package to send SMS over Twilio service.

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

ABP Community Talks
Low-Code, High Precision
13 Aug, 17:00
Online
Watch the Event
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.