nepton / EasySms

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EasySms

Build status CodeQL GitHub issues GitHub license

Template based aliyun sms sender service.

Nuget packages

Name Version Downloads
EasySms.Abstractions nuget stats
EasySms.Aliyun nuget stats

Usage

We are support Aliyun SMS service currently.

Aliyun SMS

Configure follow settings in appsettins.json

{
  "AliyunSms": {
    "ApiKey": "{Your api key}",
    "ApiSecret": "<Your api secret>",
    "SignName": "<Your signed>",
    "Templates": {
      "<TemplateName>": "<TemplateCode in aliyun>",
      "Login": "SMS_138011122"
    }
  }
}

Add service in Startup.cs

services.AddAliyunSms();

Then you can use it in your code like this:

class YourService
{
    private readonly ISmsSender _sender;

    public YourService(ISmsSender sender)
    {
        _sender = sender;
    }
    
    public async Task SendSmsAsync()
    {
        var template = new SmsTemplate("SMS_138011122", new {code = "123456"});
        var phoneNumber = "13800138000";
        await _sender.SendAsync(phoneNumber, template);
    }
}

This example will use template SMS_138011122 send verification code 123456 to phone number '13512345678' by aliyun sms service

The templateName must declared to Templates in appsettings.json, service will get the code SMS_138011122 by template name Login and send code 123456 to aliyun.

The parameter will serialize using Json.NET to a string, then pass to aliyun. The struct of parameter MUST equals with your template SMS_138011122

Final

Leave a comment on GitHub if you have any questions or suggestions.

Turn on the star if you like this project.

License

This project is licensed under the MIT License

About

License:MIT License


Languages

Language:C# 100.0%