Sunday 26 July 2015

Reading emails using c# and POP3

I wanted to write my own automated behaviour after I receive new email and for that I needed to read emails that I get.

I have used OpenPop Nuget package.

Here is my package.config file

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="OpenPop.NET" version="2.0.6.1116" targetFramework="net45" />
</packages> 


Now after installing the package I have needed to add implementation that I took from
https://github.com/foens/hpop/blob/master/OpenPopExamples/Examples.cs to read emails.

Here is my code with configuration.

 // The client disconnects from the server when being disposed
            using (Pop3Client client = new Pop3Client())
            {
                // Connect to the server
                client.Connect("pop.gmail.com", 995, true);

                // Authenticate ourselves towards the server
                client.Authenticate("email@gmail.com", "password");

                // Get the number of messages in the inbox
                int messageCount = client.GetMessageCount();

                // Most servers give the latest message the highest number
                for (int i = messageCount; i > 0; i--)
                {
                    var msg = client.GetMessage(i);
                   Console.WriteLine(msg.Headers.Subject));
                }
            }

Now the issue is that email does not allow me to read the emails.
And the reading fill fail with exception.
If you drill into the exception you will be able to find following link

https://www.google.com/settings/security/lesssecureapps

This will give you configuration option to configure this


Now when I run the code I get my emails through.


No comments:

Post a Comment