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.
Add ViewModel
Add class inside ViewModel as POPEmail.
- [Serializable]
- public class POPEmail
- {
- public POPEmail()
- {
- this.Attachments = new List<Attachment>();
- }
- public int MessageNumber { get; set; }
- [AllowHtml]
- public string From { get; set; }
- [AllowHtml]
- public string Subject { get; set; }
- [AllowHtml]
- public string Body { get; set; }
- public DateTime DateSent { get; set; }
- [AllowHtml]
- public List<Attachment> Attachments { get; set; }
- }
- [Serializable]
- public class Attachment
- {
- public string FileName { get; set; }
- public string ContentType { get; set; }
- public byte[] Content { get; set; }
- }
Controller Code
Include the package inside Controller code.
- using OpenPop.Mime;
- using OpenPop.Pop3;
Once the package is added, we can use the class 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).
- pop3Client.Connect(“ServerName”, Port, SSL);
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).
- 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.
- int count = pop3Client.GetMessageCount();
- var Emails = new List<POPEmail>();
Now, the complete code to bind the data into emails variable looks like the following one.
- int counter = 0;
- for (int i = count; i >= 1; i--)
- {
- Message message = pop3Client.GetMessage(i);
- POPEmail email = new POPEmail()
- {
- MessageNumber = i,
- Subject = message.Headers.Subject,
- DateSent = message.Headers.DateSent,
- From = string.Format("<a href = 'mailto:{1}'>{0}</a>", message.Headers.From.DisplayName, message.Headers.From.Address),
- };
- MessagePart body = message.FindFirstHtmlVersion();
- if (body != null)
- {
- email.Body = body.GetBodyAsText();
- }
- else
- {
- body = message.FindFirstPlainTextVersion();
- if (body != null)
- {
- email.Body = body.GetBodyAsText();
- }
- }
- List<MessagePart> attachments = message.FindAllAttachments();
-
- foreach (MessagePart attachment in attachments)
- {
- email.Attachments.Add(new Attachment
- {
- FileName = attachment.FileName,
- ContentType = attachment.ContentType.MediaType,
- Content = attachment.Body
- });
- }
- Emails.Add(email);
- counter++;
- if (counter > 2)
- {
- break;
- }
- }
- var emails = Emails;
- 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.
- @model IEnumerable<SendingEmailsWithWebMailInMVC.ViewModels.POPEmail>
-
- <table class="table">
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.MessageNumber)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.From)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Subject)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Body)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.DateSent)
- </th>
- <th></th>
- </tr>
-
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.MessageNumber)
- </td>
- <td>
- @Html.Raw(item.From)
- </td>
- <td>
- @Html.Raw(item.Subject)
- </td>
- <td>
- @Html.Raw(item.Body)
- </td>
- <td>
- @Html.Raw(item.DateSent)
- </td>
- </tr>
- }
-
- </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.