Sunday, November 14, 2010

Reading emails from POP server in asp.net

In this article, I will explain you how to read email from POP3 server or read emails from POP3 server which require SSL. Although I have not developed my own library, I prefer to utilize the available resource.

Well, while being in the development, you have very limited time constraint to go and research on POP3 and then develop your own component.

It is sometime wise decision to go and do the Googling and find out your requirement.

After all we, as developer should "do smart work, not hardwork" J

Anyway, for those who have kind of requirement like reading emails from POP3 server from asp.net, reading email from email server in asp.net etc. Here is the article that you probably find it useful.

Well I had requirement in couple of project where I need to collect emails that are there in particular email's Inbox. Earlier I was used to with component "Indy.Sockets" component, which really help me to achieve my task.

You may find it from here.

www.indyproject.org/

It is an open source socket library. It supports may protocols like SMTP, POP3, NNTP, HTTP, and many more.

Then in one of the module that I come to develop, I need to read Inbox of a Gmail account…!

Well as you know, Gmail requires SSL.

But the drawback with component is it does not support SSL.

So I discover Mailsystem.NET.

MailSystem is a suite of .NET components that provide users with an extensive set of email tools.

I will not explain more in detail on this, rather I will rather just explain how to integrate it and use it.

You can find more information from here..

http://www.agilecomponents.com/

Once you download it and extract it, you will require to do following steps..

  1. Navigate to "Release" folder in extracted folder.
  2. Find these DLLs: ActiveUp.Net.Mail.dll, ActiveUp.Net.Common.dll, ActiveUp.Net.Dns.dll, ActiveUp.Net.Pop3.dll.
  3. Add reference of these DLLs into your file.

Hurray… are almost done with you configuration.

Now, you only just need to develop your script that fetched and read emails from POP3 server.

So for that, here is the readymade script for you J.

public string[] GetDeliveryFailureEMailsAddress()

{

Pop3Client pop = new Pop3Client();

System.Collections.Generic.List<string> emails = new System.Collections.Generic.List<string>();

try {

// Connect to the pop3 client

pop.ConnectSsl(SMTPHost, Port, SMTPUserId, SMTPPass);

MessageCollection mc = new MessageCollection();

for (int n = 1; n <= pop.MessageCount; n++) {

Message newMessage = pop.RetrieveMessageObject(n);

//do your stuffs here using newMessage object.

}

} catch (Pop3Exception pexp) {

} finally {

if (pop.IsConnected) {

pop.Disconnect();

}

}

return emails.ToArray();

}

One important part in this script is object initialization.

// Connect to the pop3 client

pop.ConnectSsl(SMTPHost, Port, SMTPUserId, SMTPPass);

As per Gmail, you need to set:

Host: pop.gmail.com

Port: 995

As I told earlier, I want to read email from POP3 server that requires SSL in asp.net.

There are different overloaded constructors that you can use to connect to POP3 server from asp.net.

The library itself is very rich. You can achieve following functionality with POP3 server.

  • Read/Read email from POP3 server in asp.net with CRAM authentication.
  • Retrieve/Read email from POP3 server in asp.net asynchronously.
  • Retrieve/Read email from POP3 server in asp.net with secure connection, like SSL in Gmail.
  • Retrieve/Read only email count from POP3 server in asp.net.
  • Retrieve/Read only email header from POP3 server in asp.net.
  • Retrieve/Read full emails from POP3 server in asp.net.
  • Retrieve/Read full emails to a file from POP3 server in asp.net.
 

1 comment:

Anonymous said...

Thanks for that artical it was very useful in showing how implement readubg SSL Pop3 with Mailsystem.NET.

M Ranasinghe