HMENetCore.Mail 6.0.47

There is a newer version of this package available.
See the version list below for details.
dotnet add package HMENetCore.Mail --version 6.0.47
                    
NuGet\Install-Package HMENetCore.Mail -Version 6.0.47
                    
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="HMENetCore.Mail" Version="6.0.47" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HMENetCore.Mail" Version="6.0.47" />
                    
Directory.Packages.props
<PackageReference Include="HMENetCore.Mail" />
                    
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 HMENetCore.Mail --version 6.0.47
                    
#r "nuget: HMENetCore.Mail, 6.0.47"
                    
#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 HMENetCore.Mail@6.0.47
                    
#: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=HMENetCore.Mail&version=6.0.47
                    
Install as a Cake Addin
#tool nuget:?package=HMENetCore.Mail&version=6.0.47
                    
Install as a Cake Tool

HMENetCore.Mail

简介

HMENetCore.Mail 本封装库基于 MailKit 库,提供了对 IMAP/POP3 和 SMTP 协议的完整封装,实现了邮件的收取、发送、管理等功能。

功能特性

● IMAP 客户端:用于连接和操作 IMAP 邮件服务器,支持邮件收取、删除、移动、标记等操作。 ● SMTP 客户端:用于连接和操作 SMTP 邮件服务器,支持带附件、HTML格式邮件发送功能。 ● 邮件验证:提供对 IMAP、SMTP 和 POP3 服务器的连接和认证验证功能。 ● 依赖注入支持:支持通过 .NET Core 的依赖注入系统进行配置和注入。

.NET支持

  • 跨平台支持: Supports .NET Standard 2.1, .NET 6, .NET 8, and .NET 9.

安装

dotnet add package HMENetCore.Mail --version 6.0.47

服务注册

//IMAP 客户端配置
services.AddSingleton<ImapMailClient>(serviceProvider =>
{
    var config = new MailConfigEntity
    {
        Host = "imap.example.com",
        Port = 993,
        IsSsl = true,
        Account = "your_account@example.com",
        Password = "your_password"
    };
    return new ImapMailClient(config);
});

//SMTP 客户端配置
services.AddSingleton<SendMailClient>(serviceProvider =>
{
    var config = new MailConfigEntity
    {
        Host = "smtp.example.com",
        Port = 587,
        IsSsl = true,
        Account = "your_account@example.com",
        Password = "your_password"
    };
    return new SendMailClient(config);
});

//邮件服务配置
services.AddSingleton<MailService>(serviceProvider =>
{
    var config = new MailConfigEntity
    {
        Host = "imap.example.com", // 或 smtp.example.com
        Port = 993, // 或 587
        IsSsl = true,
        Account = "your_account@example.com",
        Password = "your_password"
    };
    return new MailService(config);
});

使用示例

1. IMAP 客户端使用示例

public class ImapExample
{
    private readonly ImapMailClient _imapClient;

    public ImapExample(ImapMailClient imapClient)
    {
        _imapClient = imapClient;
    }

    // 连接 IMAP 服务器并获取邮件列表
    public async Task GetEmailListAsync()
    {
        try
        {
            // 连接到 IMAP 服务器
            await _imapClient.ConnectIMAPAsync();

            // 获取收件箱中的邮件列表
            var folder = _imapClient.GetMailFolderAsync("Inbox").Result;
            var messages = folder.Fetch(0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.Envelope).ToList();

            foreach (var message in messages)
            {
                Console.WriteLine($"Subject: {message.Envelope.Subject}");
                Console.WriteLine($"From: {message.Envelope.From.Mailboxes.FirstOrDefault()?.Address}");
                Console.WriteLine($"Date: {message.Envelope.Date.Value.DateTime}");
                Console.WriteLine("----------------------------------");
            }

            // 断开连接
            _imapClient.DisconnectIMAP();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }

    //删除指定邮件
    public async Task DeleteEmailAsync(uint uniqueId)
    {
        try
        {
            // 连接到 IMAP 服务器
            await _imapClient.ConnectIMAPAsync();

            // 获取收件箱
            var folder = _imapClient.GetMailFolderAsync("Inbox").Result;
            folder.Open(FolderAccess.ReadWrite);

            // 删除指定邮件
            var uniqueIds = new List<uint> { uniqueId };
            _imapClient.DeleteEmailAsync(uniqueIds, "Inbox").Wait();

            // 断开连接
            _imapClient.DisconnectIMAP();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }

    //批量删除邮件
    public async Task BatchDeleteEmailsAsync(List<uint> uniqueIds)
    {
        try
        {
            await _imapClient.ConnectIMAPAsync();
            var folder = _imapClient.GetMailFolderAsync("Inbox").Result;
            folder.Open(FolderAccess.ReadWrite);

            _imapClient.DeleteEmailAsync(uniqueIds, "Inbox").Wait();

            folder.Close();
            _imapClient.DisconnectIMAP();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }

    //标记邮件为已读
    public async Task MarkEmailAsReadAsync(uint uniqueId)
    {
        try
        {
            await _imapClient.ConnectIMAPAsync();
            var folder = _imapClient.GetMailFolderAsync("Inbox").Result;
            folder.Open(FolderAccess.ReadWrite);

            var uniqueIds = new List<uint> { uniqueId };
            _imapClient.SetFlagAsync(uniqueIds, 1, "Inbox").Wait(); // 1 = 已读

            folder.Close();
            _imapClient.DisconnectIMAP();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }

}

2. SMTP 客户端使用示例

public class SmtpExample
{
    private readonly SendMailClient _smtpClient;

    public SmtpExample(SendMailClient smtpClient)
    {
        _smtpClient = smtpClient;
    }

    //发送邮件
    public async Task SendEmailAsync()
    {
        try
        {
            // 准备邮件内容
            var mailBody = new MailBodyEntity
            {
                Senders = new List<MailAddress> { new MailAddress { DisplayName = "Sender", Address = "sender@example.com" } },
                Recipients = new List<MailAddress> { new MailAddress { DisplayName = "Recipient", Address = "recipient@example.com" } },
                Subject = "Test Email",
                BodyHTML = "<h1>Hello, World!</h1>",
                IsReceipt = false
            };

            // 发送邮件
            var result = await _smtpClient.SendMailAsync(mailBody);

            if (result.status)
            {
                Console.WriteLine("Email sent successfully.");
            }
            else
            {
                Console.WriteLine($"Failed to send email: {result.message}");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }
}

3. 邮件验证使用示例

public class MailVerifyExample
{
    private readonly MailVerifyUtil _mailVerifyUtil;

    public MailVerifyExample(MailVerifyUtil mailVerifyUtil)
    {
        _mailVerifyUtil = mailVerifyUtil;
    }

    //验证 IMAP 服务器连接
    public async Task VerifyImapServerAsync()
    {
        try
        {
            var config = new MailConfigEntity
            {
                Host = "imap.example.com",
                Port = 993,
                IsSsl = true,
                Account = "your_account@example.com",
                Password = "your_password"
            };

            var result = await _mailVerifyUtil.ImapVerifyAsync(config);

            if (result.status)
            {
                Console.WriteLine("IMAP server verification successful.");
            }
            else
            {
                Console.WriteLine($"IMAP server verification failed: {result.message}");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  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.  net10.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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
6.0.48 89 8/11/2025
6.0.47 86 8/11/2025
6.0.46 184 8/7/2025
6.0.45 118 7/16/2025
6.0.42 152 6/18/2025
6.0.41 278 6/10/2025
6.0.40 157 5/19/2025
6.0.39 230 5/15/2025
6.0.36 149 5/7/2025
6.0.33 170 4/24/2025
6.0.32 181 4/9/2025
6.0.31 160 3/17/2025
6.0.30 120 2/19/2025
6.0.15 117 2/5/2025
6.0.12 123 12/10/2024
6.0.8 125 11/15/2024
6.0.2 122 10/9/2024
6.0.1 168 8/16/2024