Ansely.Utility.EmailSender 1.1.0

dotnet add package Ansely.Utility.EmailSender --version 1.1.0                
NuGet\Install-Package Ansely.Utility.EmailSender -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ansely.Utility.EmailSender" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ansely.Utility.EmailSender --version 1.1.0                
#r "nuget: Ansely.Utility.EmailSender, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Ansely.Utility.EmailSender as a Cake Addin
#addin nuget:?package=Ansely.Utility.EmailSender&version=1.1.0

// Install Ansely.Utility.EmailSender as a Cake Tool
#tool nuget:?package=Ansely.Utility.EmailSender&version=1.1.0                

Ansely.Utility.EmailSender

概念:

  • 一个简单方便的邮件发送包
  • 可以自定义邮件模板,以便在调用具体的Send方法时直接选择某个模板并发送

使用:

1. 使用内置的邮件模板发送邮件

// 创建EmailSender实例并配置相关设置
IEmailSender sender = new EmailSender(new EmailSenderOptions
{
    FromAddr = "xxx@xxx.com", // 发送源地址
    Secret = "xxx",	// 邮件服务器密钥
    Host = "xxx"	// 邮件服务器地址
});

// 创建内置邮件模板,传入邮件显示名称、邮件标题以及邮件内容
var tmp = new EmailTemplate("fromDisplayName", "subject", "body");

// 发送目的地址
var list = new List<string>
{
    "xxx@xxx.com"
};

// 发送
var res = sender.Send(tmp, list);
if(res.Successed)
{
    Console.WriteLine("Successful!");
}
else
{
    foreach(var i in res.Errors)
    {
        Console.WriteLine(i.Description);
    }
}

2. 自定义邮件模板

  • 只需新建一个类并继承自 AbstractEmailTemplate 抽象类
  • 你将重写如下3个较为重要的属性
  • 另外, AbstractEmailTemplate 抽象类中还定义了一个 MailMessage 类型的属性,有关更多邮件消息的配置可自定义实现
public class CustomerEmailTemplate : AbstractEmailTemplate
{
    public CustomerEmailTemplate()
    {
        // Do something...
    	this.MailMessage.IsBodyHtml = true;
    }

    public override string? FromDisplayName { get; protected set; } 
        = "xxx";
    public override string? Subject { get; protected set; } 
        = "xxx";
    public override string? Body { get; protected set; } 
        = "xxx";
}

3. 有关在Web应用程序中使用的范例

  • 包中添加了对Web应用程序依赖注入的扩展支持。它创建了一个Scope生命周期的 IEmailSender 对象
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEmailSender(options =>
{
    options.Host = builder.Configuration["Email:Host"];
    options.Secret = builder.Configuration["Email:Secret"];
    options.FromAddr = builder.Configuration["Email:FromAddr"];
});
  • 在控制器中使用如下
public class HomeController : Controller
{
    private readonly IEmailSender emailSender;

    public HomeController(IEmailSender emailSender)
    {
        this.emailSender = emailSender;
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 340 1/30/2023

Chinese notes have been added.