{"id":13430909,"url":"https://github.com/jstedfast/MailKit","last_synced_at":"2025-03-16T06:31:40.035Z","repository":{"id":12353509,"uuid":"14999623","full_name":"jstedfast/MailKit","owner":"jstedfast","description":"A cross-platform .NET library for IMAP, POP3, and SMTP.","archived":false,"fork":false,"pushed_at":"2024-04-29T14:25:26.000Z","size":23944,"stargazers_count":5942,"open_issues_count":6,"forks_count":801,"subscribers_count":210,"default_branch":"master","last_synced_at":"2024-05-02T00:17:04.080Z","etag":null,"topics":["c-sharp","dkim","email","imap","imap-client","mime","mime-parser","pgp","pop3","pop3-client","smime","smtp","smtp-client"],"latest_commit_sha":null,"homepage":"http://www.mimekit.net","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jstedfast.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"jstedfast"}},"created_at":"2013-12-07T04:07:06.000Z","updated_at":"2024-05-05T08:36:15.739Z","dependencies_parsed_at":"2023-09-28T02:15:34.882Z","dependency_job_id":"fc2df4ff-c6cb-44be-9116-ee1bab48bdda","html_url":"https://github.com/jstedfast/MailKit","commit_stats":null,"previous_names":[],"tags_count":112,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jstedfast%2FMailKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jstedfast%2FMailKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jstedfast%2FMailKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jstedfast%2FMailKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jstedfast","download_url":"https://codeload.github.com/jstedfast/MailKit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221656473,"owners_count":16858777,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-sharp","dkim","email","imap","imap-client","mime","mime-parser","pgp","pop3","pop3-client","smime","smtp","smtp-client"],"created_at":"2024-07-31T02:00:58.893Z","updated_at":"2025-03-16T06:31:40.025Z","avatar_url":"https://github.com/jstedfast.png","language":"C#","readme":"# MailKit\n\n|  Package  |Latest Release|Latest Build|\n|:----------|:------------:|:----------:|\n|**MimeKit**|[![MimeKit NuGet](https://img.shields.io/nuget/v/MimeKit.svg?logo=nuget\u0026style=flat-square)](https://www.nuget.org/packages/MimeKit)![MimeKit NuGet Downloads](https://img.shields.io/nuget/dt/MimeKit.svg?style=flat-square)|[![MimeKit MyGet](https://img.shields.io/myget/mimekit/v/MimeKit.svg?logo=nuget\u0026style=flat-square\u0026label=myget)](https://www.myget.org/feed/mimekit/package/nuget/MimeKit)|\n|**MimeKitLite**|[![MimeKitLite NuGet](https://img.shields.io/nuget/v/MimeKitLite.svg?logo=nuget\u0026style=flat-square)](https://www.nuget.org/packages/MimeKitLite)![MimeKitLite NuGet Downloads](https://img.shields.io/nuget/dt/MimeKitLite.svg?style=flat-square)||\n|**MailKit**|[![MailKit NuGet](https://img.shields.io/nuget/v/MailKit.svg?logo=nuget\u0026style=flat-square)](https://www.nuget.org/packages/MailKit)![MailKit NuGet Downloads](https://img.shields.io/nuget/dt/MailKit.svg?style=flat-square)|[![MailKit MyGet](https://img.shields.io/myget/mimekit/v/MailKit.svg?logo=nuget\u0026style=flat-square\u0026label=myget)](https://www.myget.org/feed/mimekit/package/nuget/MailKit)|\n|**MailKitLite**|[![MailKitLite NuGet](https://img.shields.io/nuget/v/MailKitLite.svg?logo=nuget\u0026style=flat-square)](https://www.nuget.org/packages/MailKitLite)![MailKitLite NuGet Downloads](https://img.shields.io/nuget/dt/MailKitLite.svg?style=flat-square)||\n\n\n|  Platform   |Build Status|Code Coverage|Static Analysis|\n|:------------|:----------:|:-----------:|:-------------:|\n|**Linux/Mac**|[![Build Status](https://github.com/jstedfast/MailKit/actions/workflows/main.yml/badge.svg?event=push)](https://github.com/jstedfast/MailKit/actions/workflows/main.yml)|[![Code Coverage](https://coveralls.io/repos/jstedfast/MailKit/badge.svg?branch=master)](https://coveralls.io/r/jstedfast/MailKit?branch=master)|[![Static Analysis](https://scan.coverity.com/projects/3202/badge.svg)](https://scan.coverity.com/projects/3202)|\n|**Windows**  |[![Build Status](https://github.com/jstedfast/MailKit/actions/workflows/main.yml/badge.svg?event=push)](https://github.com/jstedfast/MailKit/actions/workflows/main.yml)|[![Code Coverage](https://coveralls.io/repos/jstedfast/MailKit/badge.svg?branch=master)](https://coveralls.io/r/jstedfast/MailKit?branch=master)|[![Static Analysis](https://scan.coverity.com/projects/3202/badge.svg)](https://scan.coverity.com/projects/3202)|\n\n## What is MailKit?\n\nMailKit is a cross-platform mail client library built on top of [MimeKit](https://github.com/jstedfast/MimeKit).\n\n## Donate\n\nMailKit is a personal open source project that I have put thousands of hours into perfecting with the\ngoal of making it the very best email framework for .NET. I need your help to achieve this.\n\nDonating helps pay for things such as web hosting, domain registration and licenses for developer tools\nsuch as a performance profiler, memory profiler, a static code analysis tool, and more. It also helps\nmotivate me to continue working on the project.\n\n\u003ca href=\"https://github.com/sponsors/jstedfast\" _target=\"blank\"\u003e\u003cimg alt=\"Click here to lend your support to MailKit by making a donation!\" src=\"https://www.paypal.com/en_US/i/btn/x-click-but21.gif\"\u003e\u003c/a\u003e\n\n## Features\n\n* SASL Authentication\n  * Supports the following mechanisms: [CRAM-MD5](https://tools.ietf.org/html/rfc2195), [DIGEST-MD5](https://tools.ietf.org/html/rfc2831),\n  [LOGIN](https://tools.ietf.org/html/draft-murchison-sasl-login-00), [NTLM](https://davenport.sourceforge.net/ntlm.html),\n  [PLAIN](https://tools.ietf.org/html/rfc2595), [SCRAM-SHA-1[-PLUS]](https://tools.ietf.org/html/rfc5802),\n  [SCRAM-SHA-256[-PLUS]](https://tools.ietf.org/html/rfc5802), [SCRAM-SHA-512[-PLUS]](https://tools.ietf.org/html/draft-melnikov-scram-sha-512-04),\n  [OAUTHBEARER](https://tools.ietf.org/html/rfc7628) and XOAUTH2\n* Proxy Support\n  * Supports the following types of proxies: [SOCKS4/4a](https://www.openssh.com/txt/socks4.protocol), [SOCKS5](https://tools.ietf.org/html/rfc1928),\n  and [HTTP/S](https://tools.ietf.org/html/rfc2616)\n* SMTP Client\n  * Supports all of the SASL mechanisms listed above.\n  * Supports SSL-wrapped connections via the \"smtps\" protocol.\n  * Supports client SSL/TLS certificates.\n  * Supports the following extensions: [SIZE](https://tools.ietf.org/html/rfc1870), [DSN](https://tools.ietf.org/html/rfc1891),\n  [AUTH](https://tools.ietf.org/html/rfc2554), [8BITMIME](https://tools.ietf.org/html/rfc2821), [PIPELINING](https://tools.ietf.org/html/rfc2920),\n  [BINARYMIME](https://tools.ietf.org/html/rfc3030), [CHUNKING](https://tools.ietf.org/html/rfc3030), [STARTTLS](https://tools.ietf.org/html/rfc3207),\n  and [SMTPUTF8](https://tools.ietf.org/html/rfc6531)\n  * All APIs are cancellable.\n  * Async APIs are available.\n* POP3 Client\n  * Supports all of the SASL mechanisms listed above.\n  * Also supports authentication via [APOP](https://tools.ietf.org/html/rfc1939#page-15) and `USER`/`PASS`.\n  * Supports SSL-wrapped connections via the \"pops\" protocol.\n  * Supports client SSL/TLS certificates.\n  * Supports the following extensions: [TOP](https://tools.ietf.org/html/rfc1939#page-11), [UIDL](https://tools.ietf.org/html/rfc1939#page-12),\n  [EXPIRE](https://tools.ietf.org/html/rfc2449), [LOGIN-DELAY](https://tools.ietf.org/html/rfc2449), [PIPELINING](https://tools.ietf.org/html/rfc2449),\n  [SASL](https://tools.ietf.org/html/rfc2449), [STLS](https://tools.ietf.org/html/rfc2595), [UTF8](https://tools.ietf.org/html/rfc6856),\n  [UTF8=USER](https://tools.ietf.org/html/rfc6856), and [LANG](https://tools.ietf.org/html/rfc6856)\n  * All APIs are cancellable.\n  * Async APIs are available.\n* IMAP4 Client\n  * Supports all of the SASL mechanisms listed above.\n  * Supports SSL-wrapped connections via the \"imaps\" protocol.\n  * Supports client SSL/TLS certificates.\n  * Supports the following extensions: [ACL](https://tools.ietf.org/html/rfc4314), [QUOTA](https://tools.ietf.org/html/rfc2087),\n  [LITERAL+](https://tools.ietf.org/html/rfc2088), [IDLE](https://tools.ietf.org/html/rfc2177), [NAMESPACE](https://tools.ietf.org/html/rfc2342),\n  [ID](https://tools.ietf.org/html/rfc2971), [CHILDREN](https://tools.ietf.org/html/rfc3348), [LOGINDISABLED](https://tools.ietf.org/html/rfc3501),\n  [STARTTLS](https://tools.ietf.org/html/rfc3501), [MULTIAPPEND](https://tools.ietf.org/html/rfc3502), [UNSELECT](https://tools.ietf.org/html/rfc3691),\n  [UIDPLUS](https://tools.ietf.org/html/rfc4315), [CONDSTORE](https://tools.ietf.org/html/rfc4551), [ESEARCH](https://tools.ietf.org/html/rfc4731),\n  [SASL-IR](https://tools.ietf.org/html/rfc4959), [COMPRESS](https://tools.ietf.org/html/rfc4978), [WITHIN](https://tools.ietf.org/html/rfc5032),\n  [ENABLE](https://tools.ietf.org/html/rfc5161), [QRESYNC](https://tools.ietf.org/html/rfc5162), [SORT](https://tools.ietf.org/html/rfc5256),\n  [THREAD](https://tools.ietf.org/html/rfc5256), [ANNOTATE](https://tools.ietf.org/html/rfc5257), [LIST-EXTENDED](https://tools.ietf.org/html/rfc5258),\n  [ESORT](https://tools.ietf.org/html/rfc5267), [METADATA / METADATA-SERVER](https://tools.ietf.org/html/rfc5464), [NOTIFY](https://tools.ietf.org/html/rfc5465),\n  [FILTERS](https://tools.ietf.org/html/rfc5466), [LIST-STATUS](https://tools.ietf.org/html/rfc5819), [SORT=DISPLAY](https://tools.ietf.org/html/rfc5957),\n  [SPECIAL-USE / CREATE-SPECIAL-USE](https://tools.ietf.org/html/rfc6154), [SEARCH=FUZZY](https://tools.ietf.org/html/rfc6203),\n  [MOVE](https://tools.ietf.org/html/rfc6851), [UTF8=ACCEPT / UTF8=ONLY](https://tools.ietf.org/html/rfc6855), [LITERAL-](https://tools.ietf.org/html/rfc7888),\n  [APPENDLIMIT](https://tools.ietf.org/html/rfc7889), [STATUS=SIZE](https://tools.ietf.org/html/rfc8438), [OBJECTID](https://tools.ietf.org/html/rfc8474),\n  [REPLACE](https://tools.ietf.org/html/rfc8508), [SAVEDATE](https://tools.ietf.org/html/rfc8514), [XLIST](https://developers.google.com/gmail/imap_extensions),\n  and [X-GM-EXT1](https://developers.google.com/gmail/imap_extensions) (X-GM-MSGID, X-GM-THRID, X-GM-RAW and X-GM-LABELS)\n  * All APIs are cancellable.\n  * Async APIs are available.\n* Client-side sorting and threading of messages.\n\n## Goals\n\nThe main goal of this project is to provide the .NET world with robust, fully featured and RFC-compliant\nSMTP, POP3, and IMAP client implementations.\n\nAll of the other .NET IMAP client implementations that I could find suffer from major architectural\nproblems such as ignoring unexpected untagged responses, assuming that literal string tokens will\nnever be used for anything other than message bodies (when in fact they could be used for pretty\nmuch any string token in a response), assuming that the way to find the end of a message body in a\nFETCH response is by scanning for `\") UID\"`, and not properly handling mailbox names with international\ncharacters to simply name a few.\n\nIMAP requires a LOT of time spent laboriously reading and re-reading the IMAP specifications (as well\nas the MIME specifications) to understand all of the subtleties of the protocol and most (all?) of the\nother Open Source .NET IMAP libraries, at least, were written by developers that only cared enough that\nit worked for their simple needs. There's nothing necessarily wrong with doing that, but the web is full\nof half-working, non-RFC-compliant IMAP implementations out there that it was finally time for a carefully\ndesigned and implemented IMAP client library to be written.\n\nFor POP3, libraries such as OpenPOP.NET are actually fairly decent, although the MIME parser is far\ntoo strict - throwing exceptions any time it encounters a Content-Type or Content-Disposition\nparameter that it doesn't already know about, which, if you read over the mailing-list, is a problem\nthat OpenPOP.NET users are constantly running into. MailKit's Pop3Client, of course, doesn't have this\nproblem. It also parses messages directly from the socket instead of downloading the message into a\nlarge string buffer before parsing it, so you'll probably find that not only is MailKit faster (MailKit's\nMIME parser, [MimeKit](https://github.com/jstedfast/MimeKit), parses messages from disk 25x faster than\nOpenPOP.NET's parser), but also uses far less memory.\n\nFor SMTP, most developers use System.Net.Mail.SmtpClient which suits their needs more-or-less satisfactorily\nand so is probably not high on their list of needs. However, the SmtpClient implementation included with\nMailKit is a much better option if cross-platform support is needed or if the developer wants to be able to\nsave and re-load MIME messages before sending them via SMTP. MailKit's SmtpClient also supports PIPELINING\nwhich should improve performance of sending messages (although might not be very noticeable).\n\n## License Information\n\n```text\nMIT License\n\nCopyright (C) 2013-2024 .NET Foundation and Contributors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n```\n\n## Installing via NuGet\n\nThe easiest way to install MailKit is via [NuGet](https://www.nuget.org/packages/MailKit/).\n\nIn Visual Studio's [Package Manager Console](https://docs.nuget.org/docs/start-here/using-the-package-manager-console),\nenter the following command:\n\n    Install-Package MailKit\n\n## Getting the Source Code\n\nFirst, you'll need to clone MailKit from my GitHub repository. To do this using the command-line version of Git,\nyou'll need to issue the following command in your terminal:\n\n    git clone --recursive https://github.com/jstedfast/MailKit.git\n\nIf you are using [TortoiseGit](https://tortoisegit.org) on Windows, you'll need to right-click in the directory\nwhere you'd like to clone MailKit and select **Git Clone...** in the menu. Once you do that, you'll get the\nfollowing dialog:\n\n![Download the source code using TortoiseGit](https://github.com/jstedfast/MailKit/blob/master/Documentation/media/clone.png)\n\nFill in the areas outlined in red and then click **OK**. This will recursively clone MailKit onto your local machine.\n\n## Updating the Source Code\n\nOccasionally you might want to update your local copy of the source code if I have made changes to MailKit since you\ndownloaded the source code in the step above. To do this using the command-line version of Git, you'll need to issue\nthe following commands in your terminal within the MailKit directory:\n\n    git pull\n    git submodule update\n\nIf you are using [TortoiseGit](https://tortoisegit.org) on Windows, you'll need to right-click on the MailKit\ndirectory and select **Git Sync...** in the menu. Once you do that, you'll need to click the **Pull** and\n**Submodule Update** buttons in the following dialog:\n\n![Update the source code using TortoiseGit](https://github.com/jstedfast/MailKit/blob/master/Documentation/media/update.png)\n\n## Building\n\nIn the top-level MailKit directory, there are a number of solution files; they are:\n\n* **MailKit.sln** - includes the projects for .NET Framework 4.6.2/4.7/4.8, .NETStandard 2.0/2.1, .NET6.0 as well as the unit tests.\n* **MailKit.Coverity.sln** - this is used to generate Coverity static analysis builds and is not generally useful.\n* **MailKit.Documentation.sln** - this is used to generate the documentation found at https://mimekit.net/docs\n\nOnce you've opened the appropriate MailKit solution file in [Visual Studio](https://www.visualstudio.com/downloads/),\nyou can choose the **Debug** or **Release** build configuration and then build.\n\nBoth Visual Studio 2017 and Visual Studio 2019 should be able to build MailKit without any issues, but older versions such as\nVisual Studio 2015 will require modifications to the projects in order to build correctly. It has been reported that adding\nNuGet package references to [Microsoft.Net.Compilers](https://www.nuget.org/packages/Microsoft.Net.Compilers/) \u003e= 3.6.0\nand [System.ValueTuple](https://www.nuget.org/packages/System.ValueTuple/) \u003e= 4.5.0 to the MimeKit and MailKit projects will\nallow them to build successfully.\n\nNote: The **Release** build will generate the xml API documentation, but the **Debug** build will not.\n\n## Using MailKit\n\n### Sending Messages\n\nOne of the more common operations that MailKit is meant for is sending email messages.\n\n```csharp\nusing System;\n\nusing MailKit.Net.Smtp;\nusing MailKit;\nusing MimeKit;\n\nnamespace TestClient {\n    class Program\n    {\n        public static void Main (string[] args)\n        {\n            var message = new MimeMessage ();\n            message.From.Add (new MailboxAddress (\"Joey Tribbiani\", \"joey@friends.com\"));\n            message.To.Add (new MailboxAddress (\"Mrs. Chanandler Bong\", \"chandler@friends.com\"));\n            message.Subject = \"How you doin'?\";\n\n            message.Body = new TextPart (\"plain\") {\n                Text = @\"Hey Chandler,\n\nI just wanted to let you know that Monica and I were going to go play some paintball, you in?\n\n-- Joey\"\n            };\n\n            using (var client = new SmtpClient ()) {\n                client.Connect (\"smtp.friends.com\", 587, false);\n\n                // Note: only needed if the SMTP server requires authentication\n                client.Authenticate (\"joey\", \"password\");\n\n                client.Send (message);\n                client.Disconnect (true);\n            }\n        }\n    }\n}\n```\n\n## Retrieving Messages (via Pop3)\n\nOne of the other main uses of MailKit is retrieving messages from pop3 servers.\n\n```csharp\nusing System;\n\nusing MailKit.Net.Pop3;\nusing MailKit;\nusing MimeKit;\n\nnamespace TestClient {\n    class Program\n    {\n        public static void Main (string[] args)\n        {\n            using (var client = new Pop3Client ()) {\n                client.Connect (\"pop.friends.com\", 110, false);\n\n                client.Authenticate (\"joey\", \"password\");\n\n                for (int i = 0; i \u003c client.Count; i++) {\n                    var message = client.GetMessage (i);\n                    Console.WriteLine (\"Subject: {0}\", message.Subject);\n                }\n\n                client.Disconnect (true);\n            }\n        }\n    }\n}\n```\n\n## Using IMAP\n\nMore important than POP3 support is the IMAP support. Here's a simple use-case of retrieving messages from an IMAP server:\n\n```csharp\nusing System;\n\nusing MimeKit;\nusing MailKit;\nusing MailKit.Search;\nusing MailKit.Net.Imap;\n\nnamespace TestClient {\n    class Program\n    {\n        public static void Main (string[] args)\n        {\n            using (var client = new ImapClient ()) {\n                client.Connect (\"imap.friends.com\", 993, true);\n\n                client.Authenticate (\"joey\", \"password\");\n\n                // The Inbox folder is always available on all IMAP servers...\n                var inbox = client.Inbox;\n                inbox.Open (FolderAccess.ReadOnly);\n\n                Console.WriteLine (\"Total messages: {0}\", inbox.Count);\n                Console.WriteLine (\"Recent messages: {0}\", inbox.Recent);\n\n                for (int i = 0; i \u003c inbox.Count; i++) {\n                    var message = inbox.GetMessage (i);\n                    Console.WriteLine (\"Subject: {0}\", message.Subject);\n                }\n\n                client.Disconnect (true);\n            }\n        }\n    }\n}\n```\n\n### Fetching Information About the Messages in an IMAP Folder\n\nOne of the advantages of IMAP over POP3 is that the IMAP protocol allows clients to retrieve information about\nthe messages in a folder without having to first download all of them.\n\nUsing the [Fetch](https://www.mimekit.net/docs/html/Overload_MailKit_Net_Imap_ImapFolder_Fetch.htm) and\n[FetchAsync](https://www.mimekit.net/docs/html/Overload_MailKit_Net_Imap_ImapFolder_FetchAsync.htm) method overloads\n(or the convenient [extension methods](https://www.mimekit.net/docs/html/Overload_MailKit_IMailFolderExtensions_Fetch.htm)),\nit's possible to obtain any subset of summary information for any range of messages in a given folder.\n\n```csharp\nforeach (var summary in inbox.Fetch (0, -1, MessageSummaryItems.Envelope)) {\n    Console.WriteLine (\"[summary] {0:D2}: {1}\", summary.Index, summary.Envelope.Subject);\n```\n\nIt's also possible to use Fetch/FetchAsync APIs that take an [IFetchRequest](https://www.mimekit.net/docs/html/T_MailKit_IFetchRequest.htm)\nargument to get even more control over what to fetch:\n\n```csharp\n// Let's Fetch non-Received headers:\nvar request = new FetchRequest {\n    Headers = new HeaderSet (new HeaderId[] { HeaderId.Received }) {\n        Exclude = true\n    }\n};\n\nforeach (var summary in inbox.Fetch (0, -1, request)) {\n    Console.WriteLine (\"[summary] {0:D2}: {1}\", summary.Index, summary.Headers[HeaderId.Subject]);\n```\n\nThe results of a Fetch method can also be used to download individual MIME parts rather\nthan downloading the entire message. For example:\n\n```csharp\nforeach (var summary in inbox.Fetch (0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure)) {\n    if (summary.TextBody != null) {\n        // this will download *just* the text/plain part\n        var text = inbox.GetBodyPart (summary.UniqueId, summary.TextBody);\n    }\n\n    if (summary.HtmlBody != null) {\n        // this will download *just* the text/html part\n        var html = inbox.GetBodyPart (summary.UniqueId, summary.HtmlBody);\n    }\n\n    // if you'd rather grab, say, an image attachment... it might look something like this:\n    if (summary.Body is BodyPartMultipart) {\n        var multipart = (BodyPartMultipart) summary.Body;\n\n        var attachment = multipart.BodyParts.OfType\u003cBodyPartBasic\u003e ().FirstOrDefault (x =\u003e x.FileName == \"logo.jpg\");\n        if (attachment != null) {\n            // this will download *just* the attachment\n            var part = inbox.GetBodyPart (summary.UniqueId, attachment);\n        }\n    }\n}\n```\n\n### Setting Message Flags in IMAP\n\nIn order to set or update the flags on a particular message, what is actually needed is the UID or index of the message and\nthe folder that it belongs to.\n\nAn obvious reason to want to update message flags is to mark a message as \"read\" (aka \"seen\") after a user has opened a\nmessage and read it.\n\n```csharp\nfolder.Store (uid, new StoreFlagsRequest (StoreAction.Add, MessageFlags.Seen) { Silent = true });\n```\n\n### Deleting Messages in IMAP\n\nDeleting messages in IMAP involves setting a `\\Deleted` flag on a message and, optionally, expunging it from the folder.\n\nThe way to mark a message as `\\Deleted` works the same way as marking a message as `\\Seen`.\n\n```csharp\nfolder.Store (uid, new StoreFlagsRequest (StoreAction.Add, MessageFlags.Deleted) { Silent = true });\nfolder.Expunge ();\n```\n\n### Searching an IMAP Folder\n\nYou may also be interested in sorting and searching...\n\n```csharp\n// let's search for all messages received after Jan 12, 2013 with \"MailKit\" in the subject...\nvar query = SearchQuery.DeliveredAfter (DateTime.Parse (\"2013-01-12\"))\n    .And (SearchQuery.SubjectContains (\"MailKit\")).And (SearchQuery.Seen);\n\nforeach (var uid in inbox.Search (query)) {\n    var message = inbox.GetMessage (uid);\n    Console.WriteLine (\"[match] {0}: {1}\", uid, message.Subject);\n}\n\n// let's do the same search, but this time sort them in reverse arrival order\nvar orderBy = new [] { OrderBy.ReverseArrival };\nforeach (var uid in inbox.Sort (query, orderBy)) {\n    var message = inbox.GetMessage (uid);\n    Console.WriteLine (\"[match] {0}: {1}\", uid, message.Subject);\n}\n\n// you'll notice that the orderBy argument is an array... this is because you\n// can actually sort the search results based on multiple columns:\norderBy = new [] { OrderBy.ReverseArrival, OrderBy.Subject };\nforeach (var uid in inbox.Sort (query, orderBy)) {\n    var message = inbox.GetMessage (uid);\n    Console.WriteLine (\"[match] {0}: {1}\", uid, message.Subject);\n}\n```\n\nOf course, instead of downloading the message, you could also fetch the summary information for the matching messages\nor do any of a number of other things with the UIDs that are returned.\n\n### Navigating Folders in IMAP\n\nHow about navigating folders? MailKit can do that, too:\n\n```csharp\n// Get the first personal namespace and list the toplevel folders under it.\nvar personal = client.GetFolder (client.PersonalNamespaces[0]);\nforeach (var folder in personal.GetSubfolders (false))\n    Console.WriteLine (\"[folder] {0}\", folder.Name);\n```\n\nIf the IMAP server supports the SPECIAL-USE or the XLIST (GMail) extension, you can get a hold of\nthe pre-defined All, Drafts, Flagged (aka Important), Junk, Sent, Trash, etc folders like this:\n\n```csharp\nif ((client.Capabilities \u0026 (ImapCapabilities.SpecialUse | ImapCapabilities.XList)) != 0) {\n    var drafts = client.GetFolder (SpecialFolder.Drafts);\n} else {\n    // maybe check the user's preferences for the Drafts folder?\n}\n```\n\nIn cases where the IMAP server does *not* support the SPECIAL-USE or XLIST extensions, you'll have to\ncome up with your own heuristics for getting the Sent, Drafts, Trash, etc folders. For example, you\nmight use logic similar to this:\n\n```csharp\nstatic string[] CommonSentFolderNames = { \"Sent Items\", \"Sent Mail\", \"Sent Messages\", /* maybe add some translated names */ };\n\nstatic IFolder GetSentFolder (ImapClient client, CancellationToken cancellationToken)\n{\n    var personal = client.GetFolder (client.PersonalNamespaces[0]);\n\n    foreach (var folder in personal.GetSubfolders (false, cancellationToken)) {\n        foreach (var name in CommonSentFolderNames) {\n            if (folder.Name == name)\n                return folder;\n        }\n    }\n\n    return null;\n}\n```\n\nUsing LINQ, you could simplify this down to something more like this:\n\n```csharp\nstatic string[] CommonSentFolderNames = { \"Sent Items\", \"Sent Mail\", \"Sent Messages\", /* maybe add some translated names */ };\n\nstatic IFolder GetSentFolder (ImapClient client, CancellationToken cancellationToken)\n{\n    var personal = client.GetFolder (client.PersonalNamespaces[0]);\n\n    return personal.GetSubfolders (false, cancellationToken).FirstOrDefault (x =\u003e CommonSentFolderNames.Contains (x.Name));\n}\n```\n\nAnother option might be to allow the user of your application to configure which folder he or she wants to use as their\nSent folder, Drafts folder, Trash folder, etc.\n\nHow you handle this is up to you.\n\n## Contributing\n\nThe first thing you'll need to do is fork MailKit to your own GitHub repository. For instructions on how to\ndo that, see the section titled **Getting the Source Code**.\n\nIf you use [Visual Studio for Mac](https://visualstudio.microsoft.com/vs/mac/) or [MonoDevelop](https://monodevelop.com),\nall of the solution files are configured with the coding style used by MailKit. If you use Visual Studio on Windows\nor some other editor, please try to maintain the existing coding style as best as you can.\n\nOnce you've got some changes that you'd like to submit upstream to the official MailKit repository,\nsend me a **Pull Request** and I will try to review your changes in a timely manner.\n\nIf you'd like to contribute but don't have any particular features in mind to work on, check out the issue\ntracker and look for something that might pique your interest!\n\n## Reporting Bugs\n\nHave a bug or a feature request? Please open a new\n[bug report](https://github.com/jstedfast/MailKit/issues/new?template=bug_report.md)\nor\n[feature request](https://github.com/jstedfast/MailKit/issues/new?template=feature_request.md).\n\nBefore opening a new issue, please search through any [existing issues](https://github.com/jstedfast/MailKit/issues)\nto avoid submitting duplicates. It may also be worth checking the\n[FAQ](https://github.com/jstedfast/MailKit/blob/master/FAQ.md) for common questions that other developers\nhave had.\n\nIf MailKit does not work with your mail server, please include a [protocol\nlog](https://github.com/jstedfast/MailKit/blob/master/FAQ.md#ProtocolLog) in your bug report, otherwise\nthere is nothing I can do to fix the problem.\n\nIf you are getting an exception from somewhere within MailKit, don't just provide the `Exception.Message`\nstring. Please include the `Exception.StackTrace` as well. The `Message`, by itself, is often useless.\n\n## Documentation\n\nAPI documentation can be found at [https://www.mimekit.net/docs](https://www.mimekit.net/docs).\n\nSome example snippets can be found in the [`Documentation/Examples`](https://github.com/jstedfast/MailKit/tree/master/Documentation/Examples) directory.\n\nSample applications can be found in the [`samples`](https://github.com/jstedfast/MailKit/tree/master/samples) directory.\n\nA copy of the XML-formatted API reference documentation is also included in the NuGet package.\n\n## .NET Foundation\n\nMailKit is a [.NET Foundation](https://www.dotnetfoundation.org/projects) project.\n\nThis project has adopted the code of conduct defined by the [Contributor Covenant](https://contributor-covenant.org/) to clarify expected behavior in our community. For more information, see the [.NET Foundation Code of Conduct](https://www.dotnetfoundation.org/code-of-conduct).\n\nGeneral .NET OSS discussions: [.NET Foundation forums](https://forums.dotnetfoundation.org)\n","funding_links":["https://github.com/sponsors/jstedfast","https://www.paypal.com/en_US/i/btn/x-click-but21.gif"],"categories":["Frameworks, Libraries and Tools","others","C# #","框架, 库和工具","Code","C#","Libraries","C\\#","Mail","邮件","Parsing"],"sub_categories":["Mail","邮件","Library","Mail and SMTP Server","GUI - other"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjstedfast%2FMailKit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjstedfast%2FMailKit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjstedfast%2FMailKit/lists"}