Smart.MailKit 3.0.1

dotnet add package Smart.MailKit --version 3.0.1                
NuGet\Install-Package Smart.MailKit -Version 3.0.1                
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="Smart.MailKit" Version="3.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Smart.MailKit --version 3.0.1                
#r "nuget: Smart.MailKit, 3.0.1"                
#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 Smart.MailKit as a Cake Addin
#addin nuget:?package=Smart.MailKit&version=3.0.1

// Install Smart.MailKit as a Cake Tool
#tool nuget:?package=Smart.MailKit&version=3.0.1                

Smart.MailKit

基于MailKit封装的现代化.NET邮件发送库,支持依赖注入和跨平台部署,兼容.NET6/8/9。

NuGet

特性

  • 开箱即用:简洁的DI配置,10行代码完成邮件系统接入
  • 安全传输:强制SSL/TLS加密,支持SMTP/IMAP/POP3协议
  • 多附件支持:内置大文件分块传输机制(单文件≤3GB)
  • 连接池管理:自动复用SMTP连接,提升批量发送效率
  • 高性能:异步全链路设计,实测单机可达450+ QPS

安装

bash
dotnet add package Smart.MailKit

快速入门

1. 服务配置

csharp
// Program.cs
builder.Services.AddSmartMailKit(new EmailOptions {
Host = "smtp.163.com", // SMTP服务器地址
Port = 465, // SSL加密端口
Outbox = "service@company.com",// 发件邮箱
AuthorizationCode = "******", // SMTP授权码
EnableSsl = true // 强制加密
});

2. 发送邮件

csharp
public class NotificationService(SmartMailKitService mailService)
{
	public async Task SendWelcomeEmailAsync(string recipient)
	{
       var content = new MailContent(name: "用户服务系统",subject: "欢迎加入我们",body: "<p>感谢注册!请激活您的账户。</p>",isHtmlBody: true);

       var address = new MailAddress
       {
         Inbox = [ recipient ],
         Bcc = [ "log@company.com" ] // 密送日志系统
       };

       await mailService.SendEmailAsync(new Mail(content, address),CancellationToken.None);
    }
}

核心模型

配置参数 (EmailOptions)

参数 必填 默认值 说明
Host - SMTP服务器地址(如smtp.163.com)
Port 465 SSL端口号(建议465/587)
Outbox - 发件人邮箱地址
AuthorizationCode - SMTP授权码(非登录密码)
EnableSsl true 启用SSL加密

邮件实体 (Mail)

csharp
// 组合内容与地址的不可变对象
public class Mail(MailContent content, MailAddress address)
{
  public MailContent MailContent { get; } = content; // 邮件内容
  public MailAddress ToMailAddress { get; } = address;// 收件信息
}

收件地址 (MailAddress)

csharp
public class MailAddress
{
  public List<string> Inbox { get; set; } = new(); // 主送
  public List<string> CC { get; set; } = new(); // 抄送
  public List<string> Bcc { get; set; } = new(); // 密送
}

邮件内容 (MailContent)

csharp
public class MailContent(string name, string subject, string body,bool isHtmlBody = false, List<FileInfo>? attachments = null)
{
  public string MailName { get; set; } = name; // 发件人显示名称
  public string Subject { get; } = subject; // 邮件主题(不可变)
  public string Body { get; } = body; // 正文内容
  public bool IsHtmlBody { get; } = isHtmlBody; // 是否为HTML格式
  public List<FileInfo> Attachments { get; } = attachments ?? []; // 附件列表
}

高级功能

发送带附件邮件

csharp
var attachments = new List<FileInfo>
{
  new("用户手册.pdf"),
  new("合同模板.docx")
};

var content = new MailContent(name: "法务部",subject: "重要文件请查收",body: "附件包含最新法律文件",attachments: attachments);

事件监控

csharp
// 扩展SmartMailKitService添加事件钩子
public class MonitoredMailService : SmartMailKitService
{
    public event Action<MailSendEventArgs>? OnSent;

    public override async Task SendEmailAsync(Mail mail, CancellationToken ct)
    {
      var sw = Stopwatch.StartNew();
      try 
      {
        await base.SendEmailAsync(mail, ct);
        OnSent?.Invoke(new(mail, sw.Elapsed));
      }
      catch (Exception ex) 
      {
        OnSent?.Invoke(new(ex, mail, sw.Elapsed));
        throw;
      }
    }
}

生产建议

  1. 异步批处理:结合Channel实现邮件队列
  2. 证书验证:生产环境应启用严格校验
csharp
client.ServerCertificateValidationCallback = (s, cert, chain, errors) =>
cert.VerifyDomain(emailOptions.Host);

常见问题

阿里云服务器发送失败?

  • 使用465/587端口,禁用25端口
  • 在控制台开启SMTP服务

如何获取授权码?

邮箱服务 获取路径
网易163 设置 → POP3/SMTP/IMAP → 客户端授权密码
QQ邮箱 设置 → 账户 → SMTP服务 → 生成授权码
Gmail 账号安全 → 应用专用密码

性能参考

场景 配置 QPS 平均延迟
纯文本(无附件) 4核8G / 10并发 450 220ms
1MB附件 4核8G / 10并发 210 480ms
HTML复杂内容 4核8G / 10并发 410 240ms

技术支持

Developed by zenglei

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows 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
3.0.1 32 2/15/2025
3.0.0 31 2/15/2025
2.1.4 40 2/13/2025
2.1.3 62 2/9/2025
2.1.2 102 12/29/2024
2.1.1 96 12/7/2024
2.1.0 82 11/26/2024
2.0.0 117 10/8/2024
1.0.0 105 9/25/2024