Read Email Using PoP3 In ASP.NET

Introduction

In this article, I am going to explain how we can read emails using PoP3 protocol. The sample source code can be found on GitHub,

Note
GitHub project contains SMTP and POP3 sample.

POP stands for Post Office Protocol. POP3 is the latest version of the protocol for receiving emails with the help of the connection medium. Protocol is like a set of basic rules. The same way, POP is a set of rules which sends the request to the server and delivers the response to the client.

Install Package

To start with project code, first, we need to install OpenPop.Net from NuGet Packages.

ASP.NET

 

Add ViewModel

Add class inside ViewModel as POPEmail.

  1. [Serializable]  
  2. public class POPEmail  
  3. {  
  4.     public POPEmail()  
  5.     {  
  6.         this.Attachments = new List<Attachment>();  
  7.     }  
  8.     public int MessageNumber { get; set; }  
  9.     [AllowHtml]  
  10.     public string From { get; set; }  
  11.     [AllowHtml]  
  12.     public string Subject { get; set; }  
  13.     [AllowHtml]  
  14.     public string Body { get; set; }  
  15.     public DateTime DateSent { get; set; }  
  16.     [AllowHtml]  
  17.     public List<Attachment> Attachments { get; set; }  
  18. }  
  19.   [Serializable]  
  20.   public class Attachment  
  21.   {  
  22.       public string FileName { get; set; }  
  23.       public string ContentType { get; set; }  
  24.       public byte[] Content { get; set; }  
  25.   }  

Controller Code

Include the package inside Controller code.

  1. using OpenPop.Mime;  
  2. using OpenPop.Pop3;  

Once the package is added, we can use the class Pop3Client.

  1. Pop3Client pop3Client;  

Now, we have the object pop3Client and we can use this to call the methods from Pop3Client class. Before reading the email, we need to make a connection with server and server can be connected using Connect() method which requires ServerName, port, and SSL (String, int, bool).

  1. pop3Client.Connect(“ServerName”, Port, SSL); //SSL is true or false  

After making the connect request, we need to authenticate our request on the server. For that, we use the Authenticate() method which requires Email and Password (string, string).

  1. pop3Client.Authenticate(“Email”, “password”);  

Now, we are connected to the Server. I am using count to get the total number of emails in my inbox.

  1. int count = pop3Client.GetMessageCount(); //total count of email in MessageBox  
  2. var Emails = new List<POPEmail>(); //POPEmail type 

Now, the complete code to bind the data into emails variable looks like the following one.

  1. int counter = 0;  
  2.             for (int i = count; i >= 1; i--)  
  3.             {  
  4.                 Message message = pop3Client.GetMessage(i);  
  5.                 POPEmail email = new POPEmail()  
  6.                 {  
  7.                     MessageNumber = i,  
  8.                     Subject = message.Headers.Subject,  
  9.                     DateSent = message.Headers.DateSent,  
  10.                     From = string.Format("<a href = 'mailto:{1}'>{0}</a>", message.Headers.From.DisplayName, message.Headers.From.Address),  
  11.                 };  
  12.                 MessagePart body = message.FindFirstHtmlVersion();  
  13.                 if (body != null)  
  14.                 {  
  15.                     email.Body = body.GetBodyAsText();  
  16.                 }  
  17.                 else  
  18.                 {  
  19.                     body = message.FindFirstPlainTextVersion();  
  20.                     if (body != null)  
  21.                     {  
  22.                         email.Body = body.GetBodyAsText();  
  23.                     }  
  24.                 }  
  25.                 List<MessagePart> attachments = message.FindAllAttachments();  
  26.   
  27.                 foreach (MessagePart attachment in attachments)  
  28.                 {  
  29.                     email.Attachments.Add(new Attachment  
  30.                     {  
  31.                         FileName = attachment.FileName,  
  32.                         ContentType = attachment.ContentType.MediaType,  
  33.                         Content = attachment.Body  
  34.                     });  
  35.                 }  
  36.                 Emails.Add(email);  
  37.                 counter++;  
  38.                 if (counter > 2)  
  39.                 {  
  40.                     break;  
  41.                 }  
  42.             }  
  43.             var emails = Emails;  
  44.             return View(emails);  

View Code

By now, we have email data inside emails (POP3Email ViewModel). Next, we have to display the data on the View layer. So, the View code will look something like this.

  1. @model IEnumerable<SendingEmailsWithWebMailInMVC.ViewModels.POPEmail>  
  2.   
  3. <table class="table">  
  4.     <tr>  
  5.         <th>  
  6.             @Html.DisplayNameFor(model => model.MessageNumber)  
  7.         </th>  
  8.         <th>  
  9.             @Html.DisplayNameFor(model => model.From)  
  10.         </th>  
  11.         <th>  
  12.             @Html.DisplayNameFor(model => model.Subject)  
  13.         </th>  
  14.         <th>  
  15.             @Html.DisplayNameFor(model => model.Body)  
  16.         </th>  
  17.         <th>  
  18.             @Html.DisplayNameFor(model => model.DateSent)  
  19.         </th>  
  20.         <th></th>  
  21.     </tr>  
  22.   
  23. @foreach (var item in Model) {  
  24.     <tr>  
  25.         <td>  
  26.             @Html.DisplayFor(modelItem => item.MessageNumber)  
  27.         </td>  
  28.         <td>  
  29.             @Html.Raw(item.From)  
  30.         </td>  
  31.         <td>  
  32.             @Html.Raw(item.Subject)  
  33.         </td>  
  34.         <td>  
  35.             @Html.Raw(item.Body)  
  36.         </td>  
  37.         <td>  
  38.             @Html.Raw(item.DateSent)  
  39.         </td>  
  40.     </tr>  
  41. }  
  42.   
  43. </table>  

Summary

Wow! So, you are ready with an application which can read your emails and display on your application. You can improve the UI, like truncating the EmailBody and upon clicking on the email, it loads the email on new page or popup. This was just a basic step to read an email using POP3 protocol. I am not displaying the attachment on a table, so you can create a separate view and display the attachment from the specific email.


Similar Articles