Smart.MailKit
4.1.1
See the version list below for details.
dotnet add package Smart.MailKit --version 4.1.1
NuGet\Install-Package Smart.MailKit -Version 4.1.1
<PackageReference Include="Smart.MailKit" Version="4.1.1" />
<PackageVersion Include="Smart.MailKit" Version="4.1.1" />
<PackageReference Include="Smart.MailKit" />
paket add Smart.MailKit --version 4.1.1
#r "nuget: Smart.MailKit, 4.1.1"
#:package Smart.MailKit@4.1.1
#addin nuget:?package=Smart.MailKit&version=4.1.1
#tool nuget:?package=Smart.MailKit&version=4.1.1
Smart.MailKit
<a name="english"></a>
English
Smart.MailKit is a lightweight email sending service based on MailKit. It provides a simple API for sending emails with support for HTML bodies, attachments, and multiple recipients (CC/BCC). It is designed to be easily integrated into .NET applications using Dependency Injection.
Features
- MailKit Integration: Built on top of the robust MailKit library.
- Async Support: Fully asynchronous email sending.
- Rich Content: Support for HTML bodies and attachments.
- Multiple Recipients: Easy handling of To, CC, and BCC recipients.
- Dependency Injection: Seamless integration with
IServiceCollection. - Configuration: Flexible configuration options for SMTP settings, including SSL and certificate validation.
Installation
Install the package via NuGet:
dotnet add package Smart.MailKit
Usage Examples
1. Configuration & Registration
In your Program.cs or Startup.cs, configure the EmailOptions and register the service.
using Microsoft.Extensions.DependencyInjection;
using Smart.MailKit;
var builder = WebApplication.CreateBuilder(args);
// Configure Email Options
var emailOptions = new EmailOptions
{
Host = "smtp.example.com",
Port = 465,
EnableSsl = true,
Outbox = "sender@example.com",
AuthorizationCode = "your_auth_code_or_password",
SkipCertificateValidation = false // Set to true if using self-signed certs
};
// Register Smart.MailKit
builder.Services.AddSmartMailKit(emailOptions);
var app = builder.Build();
2. Sending an Email
Inject SmartMailKitService into your class and use SendEmailAsync to send emails.
using Smart.MailKit;
public class EmailService
{
private readonly SmartMailKitService _mailService;
public EmailService(SmartMailKitService mailService)
{
_mailService = mailService;
}
public async Task SendWelcomeEmail(string userEmail)
{
// 1. Create Mail Content
var content = new MailContent(
name: "My App Team",
subject: "Welcome to My App!",
body: "<h1>Welcome!</h1><p>We are glad to have you.</p>",
isHtmlBody: true
);
// Optional: Add attachments
// content.Attachments.Add(new FileInfo("path/to/file.pdf"));
// 2. Create Mail Address
var address = new MailAddress();
address.Inbox.Add(userEmail);
// address.CC.Add("manager@example.com");
// address.Bcc.Add("archive@example.com");
// 3. Send Email
var result = await _mailService.SendEmailAsync(content, address, CancellationToken.None);
if (result.IsSuccess)
{
Console.WriteLine("Email sent successfully!");
}
else
{
Console.WriteLine($"Failed to send email: {result.ErrorMessage}");
}
}
}
<a name="chinese"></a>
中文
Smart.MailKit 是一个基于 MailKit 封装的轻量级邮件发送服务。它提供了一套简单易用的 API,支持发送 HTML 邮件、附件以及多收件人(抄送/密送)。该库专为 .NET 应用程序设计,支持通过依赖注入轻松集成。
功能特性
- 集成 MailKit: 基于功能强大的 MailKit 库构建。
- 异步支持: 全异步的邮件发送操作。
- 丰富内容: 支持 HTML 正文和附件。
- 多收件人: 轻松处理收件人(To)、抄送(CC)和密送(BCC)。
- 依赖注入: 与
IServiceCollection无缝集成。 - 灵活配置: 灵活的 SMTP 配置选项,包括 SSL 和证书验证设置。
安装
通过 NuGet 安装:
dotnet add package Smart.MailKit
使用示例
1. 配置与注册
在 Program.cs 或 Startup.cs 中配置 EmailOptions 并注册服务。
using Microsoft.Extensions.DependencyInjection;
using Smart.MailKit;
var builder = WebApplication.CreateBuilder(args);
// 配置邮箱选项
var emailOptions = new EmailOptions
{
Host = "smtp.example.com", // SMTP 服务器地址
Port = 465, // 端口号 (SSL通常为465)
EnableSsl = true, // 启用 SSL
Outbox = "sender@example.com", // 发件人邮箱
AuthorizationCode = "your_auth_code_or_password", // 授权码或密码
SkipCertificateValidation = false // 如果使用自签名证书,可设为 true
};
// 注册 Smart.MailKit 服务
builder.Services.AddSmartMailKit(emailOptions);
var app = builder.Build();
2. 发送邮件
将 SmartMailKitService 注入到你的类中,并使用 SendEmailAsync 方法发送邮件。
using Smart.MailKit;
public class EmailService
{
private readonly SmartMailKitService _mailService;
public EmailService(SmartMailKitService mailService)
{
_mailService = mailService;
}
public async Task SendWelcomeEmail(string userEmail)
{
// 1. 创建邮件内容
var content = new MailContent(
name: "My App Team", // 发件人显示名称
subject: "Welcome to My App!", // 邮件主题
body: "<h1>Welcome!</h1><p>We are glad to have you.</p>", // 邮件正文
isHtmlBody: true // 是否为 HTML 格式
);
// 可选:添加附件
// content.Attachments.Add(new FileInfo("path/to/file.pdf"));
// 2. 创建收件地址信息
var address = new MailAddress();
address.Inbox.Add(userEmail); // 收件人
// address.CC.Add("manager@example.com"); // 抄送
// address.Bcc.Add("archive@example.com"); // 密送
// 3. 发送邮件
var result = await _mailService.SendEmailAsync(content, address, CancellationToken.None);
if (result.IsSuccess)
{
Console.WriteLine("邮件发送成功!");
}
else
{
Console.WriteLine($"邮件发送失败: {result.ErrorMessage}");
}
}
}
Developed by zenglei
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- MailKit (>= 4.14.1)
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
-
net8.0
- MailKit (>= 4.14.1)
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
-
net9.0
- MailKit (>= 4.14.1)
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
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 |
|---|---|---|
| 4.2.0 | 93 | 2/14/2026 |
| 4.1.1 | 92 | 2/11/2026 |
| 4.1.0 | 89 | 2/8/2026 |
| 4.0.2 | 95 | 12/30/2025 |
| 4.0.1 | 208 | 10/15/2025 |
| 4.0.0 | 202 | 4/5/2025 |
| 3.0.3 | 210 | 3/16/2025 |
| 3.0.2 | 170 | 2/26/2025 |
| 3.0.1 | 177 | 2/15/2025 |
| 3.0.0 | 184 | 2/15/2025 |
| 2.1.4 | 174 | 2/13/2025 |
| 2.1.3 | 168 | 2/9/2025 |
| 2.1.2 | 182 | 12/29/2024 |
| 2.1.1 | 182 | 12/7/2024 |
| 2.1.0 | 161 | 11/26/2024 |
| 2.0.0 | 196 | 10/8/2024 |
| 1.0.0 | 190 | 9/25/2024 |