https://github.com/jstedfast/MailKit
https://github.com/jstedfast/MimeKit
https://www.cnblogs.com/pengze0902/p/6562447.html
public class EmailHelp { ////// Smtp服务器地址 /// private static readonly string SmtpServer = ConfigurationManager.AppSettings["SmtpServer"]; ////// Pop服务器地址 /// private static readonly string PopServer = ConfigurationManager.AppSettings["PopServer"]; ////// Imap服务器地址 /// private static readonly string ImapServer = ConfigurationManager.AppSettings["ImapServer"]; ////// SMTP端口 /// private static readonly int SmtpPort = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]); ////// POP端口 /// private static readonly int PopPort = int.Parse(ConfigurationManager.AppSettings["PopPort"]); ////// IMAP端口 /// private static readonly int ImapPort = int.Parse(ConfigurationManager.AppSettings["ImapPort"]); ////// 邮件发送 /// /// 发送邮箱账号 /// 发送邮箱密码 /// 邮件 public static void SendEmali(string mailFromAccount, string mailPassword, MimeMessage message) { using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect(SmtpServer, SmtpPort, false); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); // Note: only needed if the SMTP server requires authentication client.Authenticate(mailFromAccount, mailPassword); client.Send(message); client.Disconnect(true); } } ////// 创建文本消息 /// /// 发件地址 /// 收件地址 /// 标题 /// 内容 /// 是否将POST上传文件加为附件 ///public static MimeMessage CreateTextMessage(MailboxAddress fromAddress, IList toAddressList , string title, string content, bool IsPostFiles = false) { var message = new MimeMessage(); message.From.Add(fromAddress); message.To.AddRange(toAddressList); message.Subject = title; //设置消息的主题 var html = new TextPart("html") { Text = content, }; var alternative = new Multipart("alternative"); alternative.Add(html); var multipart = new Multipart("mixed"); multipart.Add(alternative); if (IsPostFiles) { IList multiPartList = GetMimePartList(); foreach (var item in multiPartList) { multipart.Add(item); } } message.Body = multipart; return message; } /// /// 收邮件 /// /// 发送邮箱账号 /// 发送邮箱密码 /// 查询条件 /// 文件夹名称 ///public static IList ReceiveEmail(string mailFromAccount, string mailPassword, string folderName, SearchQuery searchQuery = null) { //打开收件箱 var folder = OpenFolder(mailFromAccount, mailPassword, folderName); //IList orderByList = new List { OrderBy.Date }; //查询所有的邮件 var uidss = folder.Search(searchQuery); IList msgList = new List (); if (uidss.Count > 0)//判断是否查询到邮件 { //获取邮件头 msgList = folder.Fetch(uidss, MessageSummaryItems.UniqueId | MessageSummaryItems.Full); } folder.Close(); return msgList; } /// /// 根据唯一号查询信件 /// /// 邮箱账号 /// 邮箱密码 /// 唯一号 /// 文件夹名称 ///public static MimeMessage GetEmailByUniqueId(string mailFromAccount, string mailPassword, uint id, string folderName) { //打开收件箱 var folder = OpenFolder(mailFromAccount, mailPassword, folderName); UniqueId emailUniqueId = new UniqueId(id); MimeMessage message = folder.GetMessage(emailUniqueId); /*将邮件设为已读*/ MessageFlags flags = MessageFlags.Seen; folder.SetFlags(emailUniqueId, flags, true); return message; } /// /// 读取上传的文件 /// ///public static IList GetMimePartList() { IList mimePartList = new List (); var current = HttpContext.Current; if (current != null) { HttpRequest request = current.Request; HttpFileCollection files = request.Files; int filesCount = files.Count; for (int i = 0; i < filesCount; i++) { HttpPostedFile item = files[i]; MimePart attachment = new MimePart(item.ContentType) { ContentObject = new ContentObject(item.InputStream, ContentEncoding.Default), ContentDisposition = new ContentDisposition(ContentDisposition.Attachment), ContentTransferEncoding = ContentEncoding.Base64, FileName = item.FileName }; mimePartList.Add(attachment); } } return mimePartList; } /// /// 打开邮箱文件夹 /// /// 邮箱账号 /// 邮箱密码 /// 文件夹名称(INBOX:收件箱名称) ///public static IMailFolder OpenFolder(string mailFromAccount, string mailPassword, string folderName) { ImapClient client = new ImapClient(); client.Connect(ImapServer, ImapPort); client.Authenticate(mailFromAccount, mailPassword); //获取所有文件夹 //List mailFolderList = client.GetFolders(client.PersonalNamespaces[0]).ToList(); var folder = client.GetFolder(folderName); //打开文件夹并设置为读的方式 folder.Open(MailKit.FolderAccess.ReadWrite); return folder; } /// /// 下载邮件附件 /// /// public static void DownFile(MimePart mimePart) { HttpContext context = HttpContext.Current; // 设置编码和附件格式 context.Response.ContentType = mimePart.ContentType.ToString(); //context.Response.ContentEncoding = Encoding.UTF8; context.Response.Charset = ""; string fileName = HttpUtility.UrlEncode(mimePart.FileName, Encoding.UTF8); context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); using (MemoryStream ms = new MemoryStream()) { mimePart.ContentObject.DecodeTo(ms); ms.Flush(); ms.Position = 0; context.Response.BinaryWrite(ms.GetBuffer()); context.Response.End(); } } }
http://www.mimekit.net/
MailKit正式替换了.NET的SmtpClient
SmtpClient的文档现已改成:“废弃(“SmtpClient及其相关类型设计很差,我们强烈建议使用和替代。”)”。这是Microsoft有史以来第二次将一个.NET类正式标为被开源软件库替代。
和的创建者是Jeffrey Stedfast,InfoQ曾在。在当时,它们已被认为是.NET上最全面的MIME和电子邮件库。
是被Microsoft接受的首个重要开源库。JSON.NET已在ASP.NET Web API中广泛使用,并被正式推荐为Web API使用的序列化类,通常可替代类。但是不同于SmtpClient的是,没有任何一个序列化类因此被标记为废弃。
client.Inbox.Open (FolderAccess.ReadOnly);var uids = client.Inbox.Search (SearchQuery.SubjectContains ("HELLO_"));if (uids.Count > 0) { var summaries = client.Inbox.Fetch (uids, MessageSummaryItems.Envelope); foreach (var summary in summaries) { if (summary.Envelope.Subject.StartsWith ("HELLO_")) return summary.Envelope.Subject; }}