Read Email Using C#

Introduction

This article will teach us how to read mail in C#. To read the email, we have used EAGetMail POP3/IMAP4 Component.

The EAGetMail POP3 & IMAP4 component allows developers to deliver reliable and feature-rich email applications. Many advanced features are supported, including TNEF parser (winmail.dat), Outlook .MSG file parser and S/MIME.

Let’s start with the code.

First, we created a new blank project in MVC with C#. Now we must install the Nuget package EAGetMail.

Install-Package EAGetMail -Version 5.2.5.1

Below, I will give some details about the email that you want to read the emails.

Outlook Office

Server Name: outlook.office365.com

Port: 143

Gmail

Server Name: imap.gmail.com

Port: 993

Outlook

Server Name: outlook.live.com

Port: 993

Let's start the code.

Create a .NET MVC Empty project, and create a Controller. 

public ActionResult Index() {
    MailServer oServer = new MailServer("Server Name", "Email Userid", "Email Password", ServerProtocol.Imap4);
    MailClient oClient = new MailClient("TryIt"); // Not Change TryIt it's a trial version
    oServer.SSLConnection = true;
    oServer.Port = 993; //Replace Here Port Number
    oClient.Connect(oServer);
    // By Default It Take INBOX, If You Want To Set Default Other Than INBOX Then Use Below 2 line
    Imap4Folder folder = FindFolder("Junk Email", oClient.GetFolders());
    oClient.SelectFolder(folder);
    oClient.GetMailInfosParam.GetMailInfosOptions |= GetMailInfosOptionType.NewOnly;
    oClient.GetMailInfosParam.GetMailInfosOptions |= GetMailInfosOptionType.DateRange; // Email filter using Date
    oClient.GetMailInfosParam.GetMailInfosOptions |= GetMailInfosOptionType.OrderByDateTime; // Email Sorting Using Date
    oClient.GetMailInfosParam.DateRange.SINCE = System.DateTime.Now; // Searching From Date
    oClient.GetMailInfosParam.DateRange.BEFORE = System.DateTime.Now.AddDays(1); //Searching To Date
    oClient.GetMailInfosParam.SenderContains = "Sender Email"; // Search By Sender Email
    oClient.GetMailInfosParam.SubjectContains = "Subject Email"; //  Search By Subject Name
    MailInfo[] infos = oClient.SearchMail("ALL TEXT \"Search In Email body\"");
    // If You Want To Search In Email Body Then Just Replace The 'Search In Email body' to Your Text Don't Change Any 'ALL TEXT' 
    //   MailInfo[] infos = oClient.GetMailInfos(); // If You Want To show All Email
    for (int i = 0; i < infos.Length; i++) {
        MailInfo info = infos[i];
        Mail oMail = oClient.GetMail(info);
        var from = oMail.From.Address;
        var body = oMail.TextBody;
    }
    return View();
}
static Imap4Folder FindFolder(string folderPath, Imap4Folder[] folders) {
    int count = folders.Length;
    for (int i = 0; i < count; i++) {
        Imap4Folder folder = folders[i];
        if (string.Compare(folder.LocalPath, folderPath, true) == 0) {
            return folder;
        }
        folder = FindFolder(folderPath, folder.SubFolders);
        if (folder != null) {
            return folder;
        }
    }
    // No folder found
    return null;
}

Conclusion

In this article, we have learned how to read email. EAGetMail gives us a great package that makes it easy to read email and also use search options. 


Similar Articles