Accessing Emails using IMAP

These days verifying the emails is quite natural in most of the applications. While doing automation for those scenarios, we might need the help of SMTP (Simple Mail Transfer Protocols) protocols.

Introduction 

These days verifying the emails is quite natural in most of the applications. While doing automation for those scenarios, we might need the help of SMTP (Simple Mail Transfer Protocols) protocols. We can use multiple protocols like SMTP, POP3, IMAP4, etc to access the emails. Each of the protocols has its own specific usages as below. 

  • SMTP – > Simple Mail Transfer Protocol. We can use this protocol when we want to send emails. 
  • POP -> Post Office Protocol. We can use this protocol to read the email from the server to the local device. So that, if we download the emails to our local, we can access the emails using POP in offline also. The limitation is that we can access only the ‘Inbox’ folder using the POP. 
  • IMAP -> Internet Message Access Protocol. This is more powerful than POP. using this, we can access the emails from the server. It also synchronizes the emails across all devices. 

In this article, we are going to see how to read emails, send and delete emails and how to verify the subject, body of the emails using IMAP in C# language.  

Installation: 

1.Create/open a C# Console app project. 

2.Right click the Project in the ‘Solution Explorer’ and click on ‘Manage NuGet Packages’ 

3.Enter the term ‘mail system’ in ‘Browse’ search box 

4.Select the ‘MailSystem.Net-trunk’ package and click on ‘Install’ button 

5.In the ‘Output’ section, check whether it is installed successfully. 

Connecting/Authenticating with the Email account: 

To access the email, first we need to connect that email account and log into our specific account. First, let us understand some key terms which are mandatory to log into our accounts: 

Mail Server: This is to declare the respective server names of the email accounts. 

  • If we want to access any Gmail account, we need to define it as ‘imap.gmail.com’ 
  • If we want to access any outlook account, we need to define it as ‘outlook.office365.com’ 

Port: This is to provide the respective port number of the protocols we use. 

  • If we use IMAP, then the port number should be 993 
  • If we use POP3, then the port number should be 995 

SSL: This is to notify that whether the given protocol uses SSL (Secure Socket Layer) protocol to authenticate the server or not. Based on SSL enabled or not, we need to use the respective method while connecting to the email account. 

Login: Username of the email account we are trying to access. 

Password: Password of the email account we are trying to access. 

Sample code: 

Method calling:  

Method definition:  

Setting up Gmail: 

1.Log into your Gmail account 

2.Click on the ‘Settings’ icon in the top corner and click on ‘Settings’ 

3.Click on the ‘Forwarding and POP/IMAP’ tab and make sure to select the ‘Enable IMAP’ option in the ‘IMAP access’ section. 

4.If you face any authentication issues or if you receive any email like below, then click on ‘Check Activity’ button 

5.Click ‘Yes, it was me’ 

6.Do the following setup also. Click on the logo of your Gmail account and click on ‘Manage your Google Account’ 

7.Click on ‘Security’ tab and scroll to the ‘Less secure app access’ section. Then, click on ‘Turn on access’ link in that section 

8.Update the setting as ‘ON’ 

Reading the email: 

Sample code:   

Actual email:   

Sending the email: 

Sample code:  

Sent box before executing the code:  

Sent box after executing the code:  

Deleting the email: 

In the installation steps, we have seen how to add the ‘Mail System’ NuGet Package. To delete the email, we need to follow the same installation steps and need to add the ‘Mail.dll’ package as below: 

After successful installation of the package, we can use the below code snippet and delete any email. The below illustrates how to delete a specific email by using the subject.  

Conclusion: 

In the above article, I have explained the frequently used protocol and NuGet Packages. However, we have lot of other ways to access emails. Based on the need, we can utilize any of the protocols and access our emails. 

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.