Smart.MailKit 4.2.0

dotnet add package Smart.MailKit --version 4.2.0
                    
NuGet\Install-Package Smart.MailKit -Version 4.2.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="Smart.MailKit" Version="4.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Smart.MailKit" Version="4.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Smart.MailKit" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Smart.MailKit --version 4.2.0
                    
#r "nuget: Smart.MailKit, 4.2.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.
#:package Smart.MailKit@4.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Smart.MailKit&version=4.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Smart.MailKit&version=4.2.0
                    
Install as a Cake Tool

Smart.MailKit

NuGet

English | 中文

<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 EmailOption and register the service.

using Microsoft.Extensions.DependencyInjection;
using Smart.MailKit;

var builder = WebApplication.CreateBuilder(args);

// Configure Email Options
var emailOptions = new EmailOption
{
    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.csStartup.cs 中配置 EmailOption 并注册服务。

using Microsoft.Extensions.DependencyInjection;
using Smart.MailKit;

var builder = WebApplication.CreateBuilder(args);

// 配置邮箱选项
var emailOptions = new EmailOption
{
    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 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. 
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
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