Jatin kacha's Blog

Reading emails from POP server in asp.net

Posted on: October 28, 2010

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.
Advertisements
Tags:

8 Responses to "Reading emails from POP server in asp.net"

[…] Reading emails from POP server in asp.net October 2010 4 […]

this code read only the new emails how 2 read all?????

Not very helpful for a beginner.Cant u explain each and everything in detail

Thanks for comment.
Can you please tell me what you wanted to know in “everything”?

I am able to fetch only first 279 emails from my one account which contains 900 emails & from another account I am able to read only 309 emails which actually contains more than 5000 emails.

More over the emails that I am able to retrieve are first 279 not the latest 279.

Please tell me what can be the problem in my case.

Hi,

There may be read email issue. I have received many reply to help that this component return only unread emails.
Actually it is witness by many that this component return only read emails only, so unread emails are skipped.

Hi..

Problem is not regarding read or unread mails. I am able to get both the mails. But their sorting order is first mail to 279.

But I want retrieving mail order starting from latest to 500

You can try to reach to project owner at: http://mailsystem.codeplex.com/
Hope they may able to help you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

My tweets

%d bloggers like this: