C#

Read Emails From Gmail And Outlook In C#

This article will help you to read an email from Gmail or outlook using EAGetMail. Also, it helps to save the attachment which is attached within that email.

Step 1: Install NuGet package EAGetMail V4.6.5.1 from tools > NuGet Package Manager > Manage Nuget Packages for Solution.

Step 2: Now Connect the  IMAP4 server using the following code. Server imap.gmail.com is used to read Gmail inbox. If you want to read the outlook inbox then you can use imap.outlook.com.

MailServer oServer = new MailServer("imap.gmail.com", "Gmail Id", "Gmail password", ServerProtocol.Imap4);
MailClient oClient = new MailClient("TryIt");

Step 3: Enable SSL connection for your server.

oServer.SSLConnection = true;
oServer.Port = 993;

If your server doesn’t allow an SSL connection then you can use the following code.

oServer.SSLConnection = false;
oServer.Port = 143;

Step 4: If you want to search email with unread flag, you have to set EAGetMail.GetMailInfosOptionType.NewOnly. Default will be EAGetMail.GetMailInfosOptionType.All.

oClient.GetMailInfosParam.GetMailInfosOptions = GetMailInfosOptionType.NewOnly;

Step 5: Connect the Mail server and read emails from the mailbox.

oClient.Connect(oServer);
MailInfo[] infos = oClient.GetMailInfos();

Step 6: Now you can read email content using mail object.

for (int i = infos.Length - 1; i > 0; i--)
{
    MailInfo info = infos[i];
    Mail oMail = oClient.GetMail(info);
}

Step 7: Below code is used to save attachments within your project.

var count = oMail.Attachments.ToList().Count;
for (int j = 0; j < count; j++)
{
    oMail.Attachments[j].SaveAs(Server.MapPath("~/Inbox") + "\\" + oMail.Attachments[j].Name, true); // true for overWrite file
}

 

Rupal Ribadiya

I am a Project Manager at Vision Infotech. I have more than 4 years of experience in the IT industry with understanding of Software Development Life Cycle (SDLC), involved in various phases like Requirements, Analysis/Design, Development and Testing in Microsoft Technology. Having experience in process of various application developments like web application in ASP.NET, C#, MVC, Web APIs, Rest API, Angular, Node, Express and React.

View Comments

  • Short term solution and that too with evaluation period license of EAGetMail. "TryIt" keyword make sure evaluation usage of EAGetMail otherwise the '"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then '"trial version expired" exception will be thrown.

  • hi ,
    apps can t writeline mail infos ,
    NOT WORKING >> no ref oMail and run infinite loop ... help :/

  • Hi Rupal!
    I have installed the package, recieved gmail ID using OAuth 2.0 Client IDs, but i recieve error "EAGetMail.MailServerException: A0002 NO [AUTHENTICATIONFAILED] Invalid credentials (Failure)" on line oClient.Connect(oServer); My IMAP is Enable and I set on unsafe apps allowed. What`s wrong? Can you help me? Thanks!

    • I think you misread this document. Here you don't need OAuth for authentication. In step 2 simply you need to replace your Gmail ID and Password while connecting Mail Server.
      Thanks

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago