Wednesday, December 7, 2011

Library for reading emails from a POP3 server

I needed to get the list of messages in a the inbox, so I can process them and save them into a database. After searching a while I found out a good free library for this purpose is OpenPop. Net . This library has a good interface and easy to use. Also is well documented, plus lots of useful examples. And one important thing, it supports SSL, so, for example you can read your emails in your gmail or live accounts.

This is a simple sample code for having your inbox in an Asp.Net page:
No need to say, you have to change the third line with your own gmail credentials:


   Dim client = New OpenPop.Pop3.Pop3Client
   client.Connect("pop.gmail.com", 995, True)
   client.Authenticate("mygmailaccountid@gmail.com""mypassword")
   Dim messageCount = client.GetMessageCount()
   Dim allMessages = New List(Of Net.Mail.MailMessage)(messageCount)
   For i = 1 To Math.Max(10, messageCount)
        Dim message = client.GetMessage(i)
        Dim mailMessage = message.ToMailMessage()
        allMessages.Add(mailMessage)
   Next
   grdInbox.DataSource = allMessages
   grdInbox.DataBind()
   client.Dispose()


No comments:

Post a Comment