Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Read all your emails anywhere with C#

The good thing about technology, is that it allows us transcend, upon our will and choice,the need to cut more and more trees to produce everyday paper.

Going Eco-Friendly is always a good and healthy choice, and instead of sending regular mail, with paper inside it, you send E-Mail. And instead of printing your Email out on a sheet of paper, you can read it on your preferable tablet device or phone.

So what if you want to read all your Emails from gmail, for example, not using browser at all. But instead a special kind of program that you can embed in a device of your choice and read all your emails electronically, like Alexa, telling Alexa Amazon “Alexa, read all my gmail emails !”

Sometimes, you have a need to read all, part, or latest received emails from your email provider, and store only needed parts and data within them in database, as well.

There could be two case scenarios. One is that you will be reading structured data and another that you will be reading unstructured email data. Most of the cases it is the last case ( unstructured ).

The question arises, why would one want to read this data ? The answer is to store part of it in our database for clients to use.

Suppose a robot sends automated emails with configuration and updated information about all printers on different networks, like their start time, shut down time, ink levels, number of papers printed, and a lot more information.

Our program should grab new emails , read them, take necessary data in them, and store that data in our database for other people to be able to see using front end web based interface.

Let’s say that printer was programmed to send the following information, without spaces or new lines in the beginning:

PRINTER ID:2233QWERTY
PRINTER NAME: SPACER
PRINTERTYPE:SUPERTYPE
TIMEOFDAY:SUNSHINE
PAPERSPRINTED:1000000
DOUBLESIDED:NO
USERS:ALL
, with the following subject line:
MY PRINTER STATS
I will use open source library, to read email, and I am going to use Gmail as test server. Particularly Gmail POP3 server.
We are also going to authenticate ourselves in a slightly different way. Gmail allows authentication via oAuth, but since I blogged a lot about oAuth, I will use another way.
We will append “recent” word before our user name, as such: “recent:[email protected]”. The word recent in gmail before user name allows us to get ALL emails for the past 30 days, downloaded or not, because in Gmail, once you download your messages once, you cannot download them again.
And again, we could have created an app, added Gmail API and used oAuth, but this way is different. It will also teach me and you how to use open source free for all library for POP and IMAP called OpenPop.NET.
First, let’s create database with single table in our SQL database:

Now let’s create our table called PRINTERSUNSHINE.

Now head to Visual Studio. I am using VS 2015. You can use any version you like. I like community versions. Create new console APP or solution, and issue this command in Tools > NoGet Package Manager > Package Manager Console:

Install-Package OpenPop.NET -Version 2.0.6.1120

Right click on project and create new folder called: EF. We are going to add our ADO.NET Data Entity Model there.Call it PRINTERSTATSFROMEMAIL, and follow step by step screens to add entity model from our newly created table to the console app.

After that, and in Program.cs, at the top add two using statements:

using OpenPop.Mime;
using OpenPop.Pop3;

This lets us use the OpenPop classes with POP3 functionality.

And below is the actual code for Program.cs with Main method that is an entry point for console apps:

We can use our imagination, or depending on requirements for example, and add this code to Windows Service, that will repeat itself each couple of seconds. Or we can create Java Script function with timer and SetTimeOut function, and with the help of XMLHttpRequest AJAX object, call our function that is in Home Controller, or at some REST endpoint. In short we can create our own API and then call it from anywhere.

Please do not forget to change email and password with your actual email and password.

Moreover, and as I mentioned before, data inside email can be structured, unstructured, HTML based email or TEXT based email, with different encoding. It happened to me once with structured data, so to say. Printer was sending structured TEXT based emails, hundreds per day, with fixed line positions, meaning heading for printer name would be on line one always. Then one day, I asked my system administrator to resend me new email from new printer that he added,in order for me to add code to match its name. Me, without  thinking, replied to his email, saying “Thanks”, that’s it.

The next day, we stopped receiving any data in database, if you query for today’s data, there is nothing in database, meaning the program did not insert anything.

I opened the original program that was doing email GET,which I wrote myself, everything seemed fine, with new additions as well, but somehow, it was throwing an exception index out of range. I also opened a temp text file which I use to store each new email in a text format, after I read it,and there is was ! The email that threw an exception was there, because the file was not overwritten with new email. What is interesting is that it was not regular email with printer statistics, but it started with eaning that it is HTML document. While all emails from printer come only plain TEXT based, for better readability and better and clearer handling of its content. Also, all lines that I hard-coded , because I mentioned that I was dealing with structured data, did the following mapping,if you wish:

Line one contains Printer Name and Printer Model

Line five contains three different printer variables that I need

And so on…

Their order never changes.

Now, this email, first of all was HTML email, meaning that  HTML tags were in text based form, like


. They were all there. And, it was not word wrapped,nor per line at all. In short, Unstructured. And this is because it came from my email, which sends emails in HTML format.

I then added a global variable, to hold which line we are in while traversing the file, and replaced each line index word with that global constant. Also, I deleted the reply,because it contained already duplicate data, and in both cases, I will not need it.

The program worked as charm. With structured data, grabbing all emails’ content, and inserting it in the database, then deleting the actual email.



This post first appeared on Thoughts On Programming, please read the originial post: here

Share the post

Read all your emails anywhere with C#

×

Subscribe to Thoughts On Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×