• EasyNotice 一款非常棒的 .NET开源消息通知组件
  • 发布于 2个月前
  • 88 热度
    0 评论
消息通知
EasyNotice 是一个基于 .NET 开源的消息通知组件,支持多种通知方式,包括邮件通知、钉钉、飞书、企业微信。EasyNotice 通过 Nuget 安装,傻瓜式配置,支持自定义发送间隔。开箱即用,可以直接集成到项目。

如何使用?
邮件通知
1.安装 Nuget 包。
Install-Package EasyNotice.Core
Install-Package EasyNotice.Email
2.修改 Startup.cs,注入 EasyNotice 服务。
public class Startup
{
    //...
    // 堆代码 duidaima.com
    public void ConfigureServices(IServiceCollection services)
    {
        //configuration
        services.AddEasyNotice(config =>
        {
            config.IntervalSeconds = 10;
            config.UseEmail(option =>
            {
                option.Host = "smtp.qq.com";//SMTP地址
                option.Port = 465;//SMTP端口
                option.FromName = "System";//发送人名字(自定义)
                option.FromAddress = "12345@qq.com";//发送邮箱
                option.Password = "passaword";//秘钥
                option.ToAddress = new List<string>()//收件人集合
                {
                    "12345@qq.com"
                };
            });
        });
    }    
}
3.使用 IEmailProvider 接口调用发送
[ApiController]
[Route("[controller]/[action]")]
public class NoticeController : ControllerBase
{
    private readonly IEmailProvider _mailProvider;
    public NoticeController(IEmailProvider provider)
    {
        _mailProvider = provider;
    }

    [HttpGet]
    public async Task SendMail([FromQuery] string str)
    {
        await _mailProvider.SendAsync(str, new Exception(str));
    }
}
钉钉通知
1.安装组件包
Install-Package EasyNotice.Core
Install-Package EasyNotice.Dingtalk
2.修改 Startup.cs
public class Startup
{
    //...
    
    public void ConfigureServices(IServiceCollection services)
    {
        //configuration
        services.AddEasyNotice(config =>
        {
            config.IntervalSeconds = 10; 
            config.UseDingTalk(option =>
            {
                option.WebHook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxx";//通知地址
                option.Secret = "secret";//签名校验
            });
        });
    }    
}
3.使用 IDingtalkProvider 接口调用发送
[ApiController]
[Route("[controller]/[action]")]
public class NoticeController : ControllerBase
{
    private readonly IDingtalkProvider _dingtalkProvider;
    public NoticeController(IDingtalkProvider dingtalkProvider)
    {
        _dingtalkProvider = dingtalkProvider;
    }

    [HttpGet]
    public async Task SendDingTalk([FromQuery] string str)
    {
        await _dingtalkProvider.SendAsync(str, new Exception(str));
    }
}
项目中如果有通知的功能时可以直接集成,非常方便 ~
项目地址:https://github.com/Bryan-Cyf/EasyNotice
用户评论