Code Monkey home page Code Monkey logo

sms's Introduction

Sms

适用于AbpBoilerplate的短信服务(Short Message Service,SMS)模块,通过简单配置即可使用,仅更改一处代码即可切换短信服务提供商。

Aliyun.Sms由阿里云Sms提供服务(https://www.aliyun.com/product/sms)

TencentCloud.Sms由腾讯云Sms提供服务(https://cloud.tencent.com/product/sms)

快速开始

使用阿里云Sms服务提供商

在项目中引用AbpBoilerplate.Aliyun.Sms

dotnet add package AbpBoilerplate.Aliyun.Sms

添加AliyunSmsModule模块依赖

[DependsOn(typeof(AliyunSmsModule))]
public class CoreModule : AbpModule

appsettings.json配置文件中,添加服务相关配置

"AliyunSms": {
    "RegionId": "cn-hangzhou",
    "AccessKey": "",
    "AccessKeySecret": ""
}
...

使用腾讯云Sms服务提供商

在项目中引用AbpBoilerplate.TencentCloud.Sms

dotnet add package AbpBoilerplate.TencentCloud.Sms

添加TencentCloudSmsModule模块依赖

[DependsOn(typeof(TencentCloudSmsModule))]
public class CoreModule : AbpModule

appsettings.json配置文件中,添加服务相关配置

"TencentCloudSms": {
    "SmsSdkAppId": "",
    "SecretId": "",
    "SecretKey": ""
}
...

使用帮助

在Service层中注入ISmsService服务即可

发送短信示例

public class CaptchaManager : DomainService
{
    private readonly ISmsService smsService;

    public CaptchaManager(ISmsService smsService)
    {
        this.smsService=smsService;
    }

    public async Task SendCaptchaAsync(string phoneNumber)
    {

        var captcha = CommonHelper.GetRandomCaptchaNumber();
        var model = new SendSmsRequest();
        model.PhoneNumbers= new string[] { phoneNumber };
        model.SignName="MatoApp";   //阿里云或腾讯云后台签名管理中设置应用名称
        model.TemplateCode="SMS_255330989";  //阿里云或腾讯云后台模板管理中设置模板
        model.TemplateParam="{'code':'"+captcha+"'}";
        await smsService.SendSmsAsync(model);
    }
}

注意!不同服务商所传参数有可能不同,如腾讯的短信模板参数为字符串数组,而阿里云为键值对

//for aliyun
model.TemplateParam= JsonConvert.SerializeObject(new { code = captcha });

//for tencent-cloud
model.TemplateParam= JsonConvert.SerializeObject(new string[] {captcha});

作者信息

作者:林小

邮箱:[email protected]

License

The MIT License (MIT)

sms's People

Contributors

jevonsflash avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.