OpenPOP.NET is an open-source, lightweight .NET library that provides an easy-to-use POP3 client and a robust MIME parser written in C#. It allows you to connect to a mail server, authenticate securely, count messages, and parse their text, HTML bodies, and attachments.
Because the POP3 protocol is fundamentally designed to fetch mail linearly rather than synchronize folders, OpenPOP.NET uses a 1-based indexing system to manage emails. 1. Installation
Install the package into your project using the NuGet Package Manager: Package-Manager> Install-Package OpenPop.NET Use code with caution. 2. Complete C# Code Implementation
Below is a structured example demonstrating how to connect to a mail server, fetch the latest emails, extract the text or HTML body, and handle download attachments.
using System; using System.Collections.Generic; using System.IO; using OpenPop.Mime; using OpenPop.Pop3; namespace EmailReaderApp { class Program { static void Main(string[] sender, EventArgs e) { // Configuration parameters string hostname = “pop.gmail.com”; // Example POP3 server int port = 995; // Standard SSL POP3 port bool useSsl = true; string username = “[email protected]”; string password = “your_app_password”; // Use App Password for Gmail/Outlook try { List emails = FetchLatestEmails(hostname, port, useSsl, username, password, 5); foreach (Message message in emails) { Console.WriteLine(\("Subject: {message.Headers.Subject}"); Console.WriteLine(\)“From: {message.Headers.From.Address}”); Console.WriteLine(\("Date: {message.Headers.DateSent}"); // Extract text body MessagePart textBody = message.FindFirstTextMessage(); if (textBody != null) { Console.WriteLine(\)“Body: {textBody.GetBodyAsText()}”); } // Process attachments List attachments = message.FindAllAttachments(); foreach (MessagePart attachment in attachments) { Console.WriteLine(\("Attachment Found: {attachment.FileName}"); // Save attachment to disk FileInfo file = new FileInfo(attachment.FileName); attachment.Save(file); } Console.WriteLine(new string('-', 40)); } } catch (Exception ex) { Console.WriteLine(\)“Error: {ex.Message}”); } } public static List FetchLatestEmails(string host, int port, bool ssl, string user, string pass, int countToFetch) { // The client automatically disconnects when disposed using (Pop3Client client = new Pop3Client()) { // 1. Connect to the mail server client.Connect(host, port, ssl); // 2. Authenticate credentials client.Authenticate(user, pass); // 3. Get total message count int totalMessages = client.GetMessageCount(); List fetchedMessages = new List(); // 4. Read messages from newest to oldest // POP3 indices are 1-based. Highest number is usually the newest email. int startIndex = totalMessages; int endIndex = Math.Max(1, totalMessages - countToFetch + 1); for (int i = startIndex; i >= endIndex; i–) { Message message = client.GetMessage(i); fetchedMessages.Add(message); } return fetchedMessages; } } } } Use code with caution. 3. Key Concepts & Core Methods How to use OpenPop to read e-mail as HTML? – Stack Overflow
Leave a Reply