Reading Emails Using IMAP and MailSystem.NET in C#

By FoxLearn 1/15/2025 2:53:28 AM   69
In this article, we will explore how to use C# along with the MailSystem.NET library to read emails from any IMAP-enabled mail server, including Gmail.

MailSystem.NET is an open-source library licensed under the GNU General Public License (GPL) that facilitates managing emails via various protocols like SMTP, POP3, IMAP, and more.

You can download the MailSystem.NET library here: MailSystem.NET GitHub.

Before proceeding, ensure that you have copied the required assemblies from the MailSystem.NET package into your /bin/ folder. Specifically, you will need the following two DLLs:

  • ActiveUp.Net.Common.dll
  • ActiveUp.Net.Imap4.dll

Create the Mail Repository

Below is a basic implementation of the mail repository class that connects to an IMAP server, authenticates, and fetches emails. The code defines methods for retrieving all emails or only unread emails from a specified mailbox.

using System;
using System.Collections.Generic;
using ActiveUp.Net.Mail;

namespace PT.MailIntegration.IMAP
{
    public class MailRepository
    {
        private Imap4Client _client = null;

        public MailRepository(string mailServer, int port, bool ssl, string login, string password)
        {
            if (ssl)
                Client.ConnectSsl(mailServer, port);
            else
                Client.Connect(mailServer, port);
            
            Client.Login(login, password);
        }

        public IEnumerable<Message> GetAllMails(string mailBox)
        {
            return GetMails(mailBox, "ALL").Cast<Message>();
        }

        public IEnumerable<Message> GetUnreadMails(string mailBox)
        {
            return GetMails(mailBox, "UNSEEN").Cast<Message>();
        }

        protected Imap4Client Client
        {
            get
            {
                if (_client == null)
                    _client = new Imap4Client();
                return _client;
            }
        }

        private MessageCollection GetMails(string mailBox, string searchPhrase)
        {
            Mailbox mails = Client.SelectMailbox(mailBox);
            return mails.SearchParse(searchPhrase);
        }
    }
}

Using the Repository

To read emails from a Gmail mailbox, connect to Gmail’s IMAP server using SSL on port 993.

For example, how to use the MailRepository class to retrieve unread emails:

using ActiveUp.Net.Mail;
using PT.MailIntegration.IMAP;

protected void TestMail()
{
    // Create repository for Gmail
    MailRepository rep = new MailRepository("imap.gmail.com", 993, true, @"[email protected]", "your-password");

    // Fetch unread emails from Inbox
    foreach (Message email in rep.GetUnreadMails("Inbox"))
    {
        Response.Write(string.Format("{0}: {1}\n\n{2}\n\n", email.From, email.Subject, email.BodyHtml.Text));

        // Check for attachments
        if (email.Attachments.Count > 0)
        {
            foreach (MimePart attachment in email.Attachments)
            {
                Response.Write(string.Format("Attachment: {0} {1}\n\n", attachment.ContentName, attachment.ContentType.MimeType));
            }
        }
    }
}

With MailSystem.NET, integrating IMAP email fetching into your C# applications is straightforward. This repository can be easily adapted to work with various email providers, including Gmail, and can be extended to support additional email handling features like marking emails as read or managing attachments.