Email parser - Reading emails using C#


Email parser is a program (Similar to outlook functionality) for reading emails from mail server like gmail, yahoo, etc., Using this program, we can easily maintain our email with attachments in sql database. I did this application using an "OpenPop.dll".

Introduction:



Email parser is a program (Similar to outlook functionality) for reading emails from mail server like gmail, yahoo, etc., Using this program, we can easily maintain our emails with attachments in sql database.


Requires:



We need the "OpenPop.dll" file. You can find out this dll in below attachment.


Steps:



1) Create a new website solution and add this dll file to your bin folder.
2) Create a new page (No need to implement any design). This page should execute from a task scheduler. (ReadEmailPage.aspx)
3) In your code-behind file (ReadEmailpage.aspx.cs). Initialize the "Pop3Client" class like this,

Pop3Client objClient = new Pop3Client();


This class has the following methods,
1)Connect – It is used for connecting your gmail server through your specified port. It has three parameters - mail server host name, port number, SSL option. The host name should be like this,
For gmail – smtp.gmail.com
For Yahoo – smtp.yahoo.mail.com
The port number is common for 110. If this port is not work please use some other port which is available in your local (995,465 etc.,)
SSL is a Boolean parameter set either true or false

2)Authenticate – It is used to authenticate your mail server user. It has two parameters - mail server username and password.

3) GetMessagecount – It is used to get your mail server mail counts.

4)GetMessage – It is used to get each mail message from server. It has one integer parameter. This parameter consider as message id. If we need to receive the message from this method. Initialize the "OpenPop.Mime.Message" class like this,


OpenPop.Mime.Message msg = objClient.GetMessage(i);


Here "i" is the message id. Based on message count using for loop we can get each message from this method. After getting the message all the details are assigned to "msg" object.

Your mail details are stored in the following attributes,
Subject - msg.Headers.Subject;
From address – msg.Headers.From.Address;
Display name – msg.Headers.From.DisplayName;
Message date - msg.Headers.Date;
CC address – msg.Headers.Cc[0].Address; CC is a collection using for loop you can get each cc address, display name.

For getting the "Body Message" you need to initialize another class named "MessagePart" like this,


OpenPop.Mime.MessagePart plainTextPart = msg.FindFirstPlainTextVersion();


get the body message from "FindFirstPlainTextVersion" method. This method returns the message as mime message part. We need to convert the mime message part to plain text. Using "GetBodyAsText" method we can get pure body message.

Message = plainTextPart.GetBodyAsText();


Before getting the body text. We should check the message part object is null or not. It may sometimes has null value. Now we red all the mail contents of each mail. All the mail attachments are stored in "FindAllAttachments" method. It returns collections of "OpenPop.Mime.MessagePart" class. Using for loop download each attachments in your local.


foreach (OpenPop.Mime.MessagePart attachment in attachments)
{
if (attachment != null)
{
string ext = attachment.FileName.Split('.')[1];
FileInfo file = new FileInfo(Server.MapPath("Attachments\\") + DateTime.Now.Ticks.ToString() + "." + ext);

attachment.SaveToFile(file);
}
}


Now we have all the mail details write your database coding methods and store the mail contents to your database. If we want to delete the red messages from server then only we need to call the delete message method otherwise don't need to call that.

5)DeleteMessage – It is used to delete a particular message from server. It has one integer parameter (Message id). It deletes the specified message id mail.

6)DeleteAllMessage – It is used to delete all the mails from server.
All the above methods are commonly used. "Connect" and "Authenticate" method should call after initialize the "Pop3Client" class. If we implement this code in "Windows" based application use thread. For web application "Task scheduler" is best method otherwise it throws the time error.

4) Create a scheduler file (.vbs). This file contains the vb script coding for executes the web page.

Call LogEntry()

Sub LogEntry()
On Error Resume Next
Dim objRequest
Dim URL
Set objRequest = CreateObject("Microsoft.XMLHTTP")
URL = "http://localhost:1032/EmailParser/ReadEmailpage.aspx"
objRequest.open "POST", URL , false
objRequest.Send
Set objRequest = Nothing
End Sub


5) Open Control panel - > Task scheduler -> Add schedule tasks - > Add your VBS file and set your schedule.

6) If you want to test the scheduler in local run your website and put the break point on your "ReadEmailpage". Right click on your task and select run. Now your page is executes and debug the program.

Herewith I attached my complete sample program with vbs file. Please find out the attachments and let me know if you have any problem to implement this.


Attachments

  • Email parser - Reading emails using C# (42151-6032-EmailParser.rar)
  • Comments

    Author: Rajasundar08 Feb 2011 Member Level: Silver   Points : 0

    Hi,
    Good Resource...Hi editor pls approve this as soon as possible..

    Author: ss.gowtham21 Jun 2011 Member Level: Silver   Points : 0

    is delete of mails in inbox is possible


    den can u give me a code to perform that task

    Author: ss.gowtham21 Jun 2011 Member Level: Silver   Points : 0

    is delete of mails in inbox is possible


    den can u give me a code to perform that task

    Author: DAMU23 Jun 2011 Member Level: Gold   Points : 1

    Hi Gowtham,

    Yes, you can delete the emails from your database only. If you need to delete the email from your inbox then just use the following code,


    objClient.DeleteMessage(MessageNumber);


    objClient is an object for POP3Client class. Refer my above article.

    Guest Author: Ritesh22 Dec 2011

    Hi ,

    I tried objClient.DeleteMessage(MessageNumber); method to delete message from Gmail account but it is not delete any of Email also can You please tell me how should i restrict not to read same email again.


    Thanks

    Guest Author: partha11 Apr 2012

    if we use OpenPop.Mime.Message msg = objClient.GetMessage(i);
    it is taken message from send box also how to avoid that pls help.

    Guest Author: lalit23 Jul 2012

    how can i identify spam message in the server

    Author: Rajesh12 Dec 2012 Member Level: Bronze   Points : 0

    Is it possible to move a mail from one folder to another using component mentioned here? or how do i move?

    Author: Ngako04 Jul 2014 Member Level: Bronze   Points : 0

    Can someone post link where i can get OpenPop.dll library please?



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: