用于 IMAP、POP3 和 SMTP 的开源 .NET 库
免费的 C# .NET 库,用于生成带有附件的消息,使用 PGP/MIME 加密/解密消息。
MailKit 入门
安装 MailKit 的最简单方法是通过 NuGet。要从 Visual Studio 的包管理器控制台使用它,请输入以下命令。
通过 NuGet 安装 Mailkit
Install-Package MailKit
通过 GitHub 安装 Mailkit
git clone --recursive https://github.com/jstedfast/MailKit.git
通过 .NET 创建新消息
开源 API MailKit 库使软件开发人员能够使用一些简单的命令创建 MIME 消息。 TextPart 是具有文本媒体类型的叶节点 MIME 部分。 TextPart 构造函数的第一个参数指定媒体子类型,在本例中为普通类型。您可能熟悉的另一种媒体子类型是 HTML 子类型。获取和设置 MIME 部分的字符串内容的最简单方法是 Text 属性。
开源 API MailKit 库使软件开发人员能够使用几个简单的命令创建 MIME 消息。 TextPart 是具有文本媒体类型的叶节点 MIME 部分。 TextPart 构造函数的第一个参数指定媒体子类型,在本例中为普通类型。您可能熟悉的另一种媒体子类型是 HTML 子类型。获取和设置 MIME 部分的字符串内容的最简单方法是 Text 属性。
使用 C# 生成和发送免费消息
var message = new MimeMessage();
message.From.Add(new MailboxAddress("fred", "This email address is being protected from spam-bots. You need JavaScript enabled to view it."));
message.To.Add(new MailboxAddress("frans", "This email address is being protected from spam-bots. You need JavaScript enabled to view it."));
message.Subject = "FileFormat ";
message.Body = new TextPart("plain")
{
Text = "File Format Developer Guide"
};
using (var client = new SmtpClient())
{
// For demo-purposes,
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("smtp.test.com", 587, false);
// Note: only needed if the SMTP server requires authentication
client.Authenticate("frans", "password");
client.Send(message);
client.Disconnect(true);
}
使用 .NET API 生成带有附件的消息
MailKit API 提供了在 .NET 应用程序中生成带有附件的消息的功能。附件就像任何其他 MimePart 一样;主要区别在于它们包含一个包含附件值的 content-disposition 标头,而不是 inline 或根本没有 Content-Disposition 标头。要发送消息的文本/HTML 和文本/纯文本版本,您需要为每个部分创建一个 TextPart,然后将它们添加到多部分/替代。
通过 C# 创建带有附件的消息
var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
message.Subject = "How you doin?";
// create our message text, just like before (except don't set it as the message.Body)
var body = new TextPart ("plain") {
Text = @"Hey Alice,
What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.
Will you be my +1?
-- Joey
"
};
// create an image attachment for the file located at path
var attachment = new MimePart ("image", "gif") {
Content = new MimeContent (File.OpenRead (path), ContentEncoding.Default),
ContentDisposition = new ContentDisposition (ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
FileName = Path.GetFileName (path)
};
// now create the multipart/mixed container to hold the message text and the
// image attachment
var multipart = new Multipart ("mixed");
multipart.Add (body);
multipart.Add (attachment);
// now set the multipart/mixed as the message body
message.Body = multipart;
使用 PGP/MIME 加密/解密消息
MailKit 库提供了在 .NET 应用程序中使用 PGP/MIME 加密电子邮件消息的功能。 PGP/MIME 使用具有 multipart/encrypted mime 类型的 MIME 部分来封装加密数据。如果要加密邮件,最好使用 SecureMailboxAddress 而不是为每个收件人使用 MailboxAddress,这将允许用户指定每个收件人的 PGP 密钥的唯一指纹。
通过 C# 使用 PGP/MIME 加密消息
public void Encrypt (MimeMessage message)
{
// encrypt our message body using our custom GnuPG cryptography context
using (var ctx = new MyGnuPGContext ()) {
// Note: this assumes that each of the recipients has a PGP key associated
// with their email address in the user's public keyring.
//
// If this is not the case, you can use SecureMailboxAddresses instead of
// normal MailboxAddresses which would allow you to specify the fingerprint
// of their PGP keys. You could also choose to use one of the Encrypt()
// overloads that take a list of PgpPublicKeys.
message.Body = MultipartEncrypted.Encrypt (ctx, message.To.Mailboxes, message.Body);
}
}
解密 PGP/MIME 消息
public MimeEntity Decrypt (MimeMessage message)
{
if (message.Body is MultipartEncrypted) {
// the top-level MIME part of the message is encrypted using PGP/MIME
var encrypted = (MultipartEncrypted) entity;
return encrypted.Decrypt ();
} else {
// the top-level MIME part is not encrypted
return message.Body;
}
}