.NET利用MailKit举办邮件处理惩罚的要领步调

MimeKit and MailKit are popular fully-featured email frameworks for .NET

框架支持版本如下

Supports .NET 4.5, .NET 4.6, .NET 4.7, .NET 4.8, .NET 5.0, .NETStandard 2.0, Xamarin.Android, Xamarin.iOS, Windows Phone 8.1, and more.

MailKit是最风行且最强大的.NET邮件处理惩罚框架之一,下面为各人简朴先容MailKit的利用方法(IMAP为例)

1. 参考资料

Github https://github.com/jstedfast/MailKit

官方Doc

2.焦点内容(IMAP为例)

毗连邮箱

加密

client.Connect("imap.exmail.qq.com", 993, SecureSocketOptions.SslOnConnect);

不加密

client.Connect("imap.exmail.qq.com", 143, SecureSocketOptions.None);

登入邮箱

client.Authenticate(MAIL_NAME, MAIL_PASSWORD);

打开邮件文件夹

client.Inbox.Open(FolderAccess.ReadWrite);

读取文件

读取方法一:可以预先筛选邮件

search for messages where the Subject header contains either "MimeKit" or "MailKit" var query = SearchQuery.SubjectContains("MimeKit").Or(SearchQuery.SubjectContains("MailKit")); var uids = client.Inbox.Search(query);

读取方法二:读取所有邮件

var uids = client.Inbox.Search(SearchQuery.All);

邮件操纵

操纵邮件一:读取邮件标题

string subject = message.Subject; if (!subject.Contains("MimeKitDemo")) return;

操纵邮件二:读取正文

string body = message.TextBody ?? string.Empty; if (!body.Contains("MimeKitDemoBody")) return;

操纵邮件三:下载邮件附件

var attachments = message.Attachments; if (attachments.Any()) { foreach (var attachment in attachments) DownloadAttachment(attachment); } private static void DownloadAttachment(MimeEntity attachment) { if (attachment is MessagePart) { var fileName = attachment.ContentDisposition?.FileName; var rfc822 = (MessagePart)attachment; if (string.IsNullOrEmpty(fileName)) fileName = "attached-message.eml"; var path = Path.Combine(DIRECTORY, fileName); using (var stream = File.Create(path)) rfc822.Message.WriteTo(stream); } else { var part = (MimePart)attachment; var fileName = part.FileName; var path = Path.Combine(DIRECTORY, fileName); using (var stream = File.Create(path)) part.Content.DecodeTo(stream); } }

操纵邮件四:移动邮件(移动至删除文件夹)

client.Inbox.MoveTo(uid, client.GetFolder(SpecialFolder.Trash));

操纵邮件五:删除邮件 - 将邮件标志为删除、最后删除

client.Inbox.AddFlags(uid, MessageFlags.Deleted, true); client.Inbox.Expunge();

3.样例源码地点

https://github.com/Impartsoft/Bins/tree/main/MailKitDemo

到此这篇关于.NET利用MailKit举办邮件处理惩罚的要领步调的文章就先容到这了,更多相关.NET MailKit邮件处理惩罚内容请搜索剧本之家以前的文章或继承欣赏下面的相关文章但愿各人今后多多支持剧本之家!

您大概感乐趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsdxwx.html