Thursday, November 7, 2013
0 comments

Email parser - Reading emails using C#

10:21 AM

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)
  • 0 comments:

     
    Toggle Footer