That's a long title but it's the best description of what I have for you today. I'll be up front about it as well, I took code from here and here to figure this out because to be honest, the MIME format seems as though it was designed by some evil genius to bring pain and destruction to the world (a decent header structure would have been nice. I understand the logic behind it all, but still some more structure would be nice). The code for both has been modified though. For instance there were a number of issues with the Pop3 client code when trying to connect to GMail, response issues, some blocking and error issues, etc. The MIME code on the other hand had other issues with regard to decoding content, couldn't tell "To" and "to" were the same thing in the header, etc. (I'm still not 100% there either), plus I wasn't a big fan of the layout of the classes (I'm not that happy with my own either but it's slowly getting there). Sadly there is too much code to place directly here. As such I suggest one of two options. First is to go to my utility library and download the DLLs (the Pop3 and MIME class are under the Utilities.Web.Email namespace). Or go to the repository here and download the latest version.
The code is divided into Pop3 (which is commented) and MIME code (which is not commented because I'm not done with it yet and I plan to refactor it completely). The classes that you're going to deal with (even though there are quite a few) are Pop3Client and MIMEMessage. Pop3Client connects to the server and has the ability to get a list of messages, their content, delete messages, and disconnect from the server. The MIMEMessage class actually contains the message (a simple ToString() on it will get the entire text of the message) and has properties for getting whom it is to, whom it is from, the subject, and the body of the message (although I don't have the decoding 100% yet on that, plus it spits out the first body text it finds which could be HTML or plain text). Anyway, lets look at some example code:
1: Utilities.Web.Email.Pop3.Pop3Client Client=new Utilities.Web.Email.Pop3.Pop3Client();
2: Client.UseSSL = true;
4: List<Utilities.Web.Email.Pop3.Message> Messages = Client.GetMessageList();
5: if (Messages.Count > 0)
7: Utilities.Web.Email.Pop3.Message Message=Client.GetMessage(Messages);
8: DivContentHolder.InnerHtml = Message.MessageBody.To + "<br />";
9: DivContentHolder.InnerHtml += Message.MessageBody.From + "<br />";
10: DivContentHolder.InnerHtml += Message.MessageBody.Subject + "<br />";
11: DivContentHolder.InnerHtml += Message.MessageBody.BodyText;
Note that DivContentHolder is simply a div that I put the information into. But all the code above does is creates a Pop3Client object, tells it we're using SSL, connects to the server (make sure to set up the login info), asks for a list of messages, and puts the first message it gets into a div on a web page after which it disconnects from the server. That's pretty much it as far as using the code. There are a few things to keep in mind though when connecting to GMail:
Google requires you to use SSL to connect to their server. That is why we set UseSSL to true. If you're connecting somewhere else, you may not need to do this.
They use pop.gmail.com as the server, and port number 995. Not 110 like most Pop3 servers (once again due to SSL).
Every time you connect message 0 may not be the same message each time.
This retrieves messages you've sent as well as messages you've received (at least in my experiments).
ALWAYS DISCONNECT PROPERLY.
That's all there is to it really. So download the code, leave feedback, and happy coding.