Connecting to GMail using Pop3, plus MIME Format Reader in C#

11/21/2008

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;
   3: Client.Connect(emailaddress,password,"pop.gmail.com",995);
   4: List<Utilities.Web.Email.Pop3.Message> Messages = Client.GetMessageList();
   5: if (Messages.Count > 0)
   6: {
   7:     Utilities.Web.Email.Pop3.Message Message=Client.GetMessage(Messages[0]);
   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;
  12: }
  13: Client.Disconnect();

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:

  1. 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.
  2. They use pop.gmail.com as the server, and port number 995. Not 110 like most Pop3 servers (once again due to SSL).
  3. Every time you connect message 0 may not be the same message each time.
  4. This retrieves messages you've sent as well as messages you've received (at least in my experiments).
  5. ALWAYS DISCONNECT PROPERLY.

That's all there is to it really. So download the code, leave feedback, and happy coding.



Comments

James Craig
September 03, 2010 9:16 AM

Actually if you go here:http://cul.codeplex.com/SourceControl/changeset/view/60327#And look under trunk->Web->Email->Pop3 as well as trunk->Web->Email->MIME, you'll find the latest versions of the code. The issues that zhongwenjie mentions haven't been fixed yet (I haven't had time to fix the MIME parser completely), but your connection issue is fixed. Sort of anyway. With gmail, they will only send the list of sent/received messages once. So if you run your code twice (or if you have something else on the same IP address that is going through POP3), it will say that you have 0 messages. But it wont hang/wait forever with the new code waiting for the list that will never come.

oom
September 02, 2010 6:45 PM

the code connected ok, but failed to retrieve the emails, is there an update to the code posted here or have any one fixed the bugs in it yet? Thanks again at least half my problem is solved........

zhongwenjie
July 31, 2010 4:37 AM

Thanks. It's a wonderful job. But There are some bugs in it. No. 1:From: =?gb2312?Q?=D6=D3=CE=C4=BD=DC?=<xxx@xxx.xxx>To: =?gb2312?Q?=D6=D3=CE=C4=BD=DC?=<xxx@xxx.xxx>Date: 31 Jul 2010 12:58:16 +0800Subject: =?gb2312?Q?=B2=E21?=Content-Type: text/plain; charset=gb2312Content-Transfer-Encoding: quoted-printableMessage-Id: <20100731050039.9A8EB133CFE@xxx.xxx>=B2=E22When decoding "=?gb2312?Q?=D6=D3=CE=C4=BD=DC?=", CurrentIndex2 have got 10, but it should be 29.No. 2:When decoding the mail-body of the example above, it will return an empty string. I replace "!string.IsNullOrEmpty(CurrentLine)"(Email\MIME\CodeTypes\CodeQP.cs:28 & 46) with "CurrentLine != null" to correct it.No. 3:When mail-body is empty, Header[Constants.ContentType] will be null. An exception will be throw at Email\MIME\MIMEBody.cs:67

Topper
May 18, 2010 10:08 AM

very big thax, happy of this. very useful and cute code