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
}

 

10 Comments

  1. Aton

    Hi,
    it doesn’t work for me
    code:

    try
    {
    MailServer oServer = new MailServer(“imap.gmail.com”, “myemail@gmail.com”, “mypassword”, ServerProtocol.Imap4);
    MailClient oClient = new MailClient(“TryIt”);
    oServer.SSLConnection = true;
    oServer.Port = 993;
    //oServer.SSLConnection = false;
    //oServer.Port = 143;
    oClient.GetMailInfosParam.GetMailInfosOptions = GetMailInfosOptionType.NewOnly;
    oClient.Connect(oServer);
    MailInfo[] infos = oClient.GetMailInfos();
    }
    catch (Exception ex)
    { }

    I have checked and my gmail imap configuration is enabled.

    Please, let me know what is wrong-
    Thx in advance

    0
    0
    Reply
  2. Joseph

    Hello,
    This software is free ?

    0
    0
    Reply
  3. Nilesh K Pujari

    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.

    0
    0
    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories