Welcome to Dream.In.Code
Become a C# Expert!

Join 137,420 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,981 people online right now. Registration is fast and FREE... Join Now!




Connecting to POP3 server

 
Reply to this topicStart new topic

Connecting to POP3 server

cygnusX
5 Jan, 2008 - 07:49 AM
Post #1

D.I.C Head
**

Joined: 19 May, 2007
Posts: 159



Thanked: 2 times
My Contributions
Does someone know how to connect to POP3 server which use SSL.I'm using the following code:

CODE
  TcpClient mail = new TcpClient();
            mail.Connect("pop.gmail.com", 995);
            NetworkStream netStream = mail.GetStream();
            StreamReader strReader = new StreamReader(netStream);

            byte[] readBuffer = new byte[1024];
            StringBuilder receivedMessage = new StringBuilder();
            int messageLength = netStream.Read(readBuffer, 0, readBuffer.Length);

            try
            {
                if (mail.Connected)
                {
                        receivedMessage.AppendFormat(Encoding.ASCII.GetString(readBuffer, 0, messageLength));
                        MessageBox.Show(receivedMessage.ToString());
                }
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }


The problem is that i'm not receiving anything from the server.Can someone help me with that?

This post has been edited by cygnusX: 5 Jan, 2008 - 07:50 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Connecting To POP3 Server
5 Jan, 2008 - 01:46 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,260



Thanked: 227 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Thanks for the wonderful question! I get to use a little used class to help you out. So here we go...

The problem you have here is that you can't simply use a TCPClient class to just hookup to port 995 and start communicating much like you would if you were communicating to gmail on something like port 110 (standard pop3).

SSL is a security protocol which sits on top of the connection and used for encryption and security. Protocols like HTTP sit on top of SSL. So you can think of it as a sandwich... stream on the bottom, SSL in the middle, pop on top (hey it rhymes!)

This solution I am about to propose requires you to at least have .NET 2.0 SP1 or above. If you are using 2005 express, make sure you are patched up with SP1 and you should be good to go. In this version of the framework they have a class called SslStream which wraps around a stream and provides this SSL support.

So you use TCPClient to generate a stream, get the stream, throw it into the SslStream class and use that as your read/write stream. Here is an example...

CODE

// Create our client and a SslStream variable (notice it is no longer NetworkStream)
TcpClient mail = new TcpClient();
SslStream netStream;

// Connect to gmail using 995 port
mail.Connect("pop.gmail.com", 995);

// Get the stream and toss it into the constructor of SslStream
// Now netStream is a SSL secured stream
netStream = new SslStream(mail.GetStream());

// Last thing you need is to authenticate as a client (you are connecting to them so you are the client and they are the server).
// You have to pass in the name on their certificate, which so happens to be the same host they are using for connecting. "pop.gmail.com"
netStream.AuthenticateAsClient("pop.gmail.com");


Once you authenticate as a client and tell them the name on their certificate, gmail deems you worthy to speak to. Just to clean a few things up, get rid of that line about StreamReader. Just read and write from netStream as you would a usual network stream.

All communications to and from Google on that stream is passed through SslStream for encryption/decryption and you are communicating with them.

There you go. Enjoy!

"At DIC we be SSL handling code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

cygnusX
RE: Connecting To POP3 Server
6 Jan, 2008 - 02:54 AM
Post #3

D.I.C Head
**

Joined: 19 May, 2007
Posts: 159



Thanked: 2 times
My Contributions
Thanks for the complete answer.That really helps me. icon_up.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 04:17AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month