How to read Gmail Email Programmatically
With my previous post I mentioned I would do a sample app to demonstrate how easy it would be to read Gmail email account programmatically using c# code and with a little help from ATOM.NET ( http://atomnet.sourceforge.net/ ).
Bellow is the Program.cs from the sample console application I created for demo purposes. I developed this using Visual Studio 2008, but the latest version was not required in order to achieve this outcome.
The code simply connects to the Gmail Atom feed here: https://mail.google.com/mail/feed/atom and sends your credentials (username, password for your email account) and logs into the account and reads the latest email in the inbox. The demo only reads 1 email. You can extend this starting on line 73.
I have put a few comments throughout the code to help with explanations, again just to illustrate how the code is working. You will notice how ATOM.NET made it very easy reading the XML feed from Google Gmail.
If you want the full Visual Studio 2008 source code of this demo, then you can have it FREE via the link below:
TestCheckGMailEmail.zip (93.4 kb)
If you would like a .NET 2.0 version of the source code then you can add a comment to this posting requesting it, or send me an email to yellow_duck_guy@hotmail.com (Note: there are underscores (_) within the email address).
1: using System;
2: using System.IO;
3: using System.Net;
4: using System.Text;
5: using Atom.Core;
6: using Atom.Core.Collections;
7:
8: namespace TestCheckGMailEmail
9: {
10: /// <summary>
11: /// Console Demo Application Code for checking Gmail Email Account
12: /// Check out my blog here: http://yellowduckguy.spaces.live.com
13: /// </summary>
14: internal class Program
15: {
16: // GMAIL URL for Atom Email Reading
17: private static readonly Uri _gmailServerUri = new Uri("https://mail.google.com/mail/feed/atom");
18:
19: private static void Main(string[] args)
20: {
21: ShowAppTitle();
22: PromptForCredentials();
23: }
24:
25: private static void PromptForCredentials()
26: {
27: Console.WriteLine("Enter User Name:");
28: string username = Console.ReadLine();
29: Console.WriteLine("Enter Password (Security Note: This will be shown on screen):");
30: string password = Console.ReadLine();
31:
32: ReadGMailEmail(username, password);
33: }
34:
35: private static void ReadGMailEmail(string username, string password)
36: {
37: // Remove data input, comment out if you want to keep the input
38: Console.Clear();
39: ShowAppTitle();
40:
41: Console.WriteLine("Now attempting login … ");
42:
43: WebRequest webRequestGetUrl;
44: try
45: {
46: // Create a new web-request instance
47: webRequestGetUrl = WebRequest.Create(_gmailServerUri);
48:
49: byte[] bytes = Encoding.ASCII.GetBytes(username + ":" + password);
50:
51: // Add the headers for basic authentication.
52: webRequestGetUrl.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(bytes));
53:
54: // Get the response feed
55: Stream feedStream = webRequestGetUrl.GetResponse().GetResponseStream();
56:
57: // Load Feed into Atom DLL
58: AtomFeed gmailFeed = AtomFeed.Load(feedStream);
59:
60: AtomEntryCollection entries = gmailFeed.Entries;
61:
62: // Clear the screen just before we tell the user more information
63: Console.Clear();
64: ShowAppTitle();
65:
66: if (entries.Count > 0)
67: {
68: Console.WriteLine(string.Format("Found {0} email(s)", entries.Count));
69: Console.WriteLine(Environment.NewLine);
70:
71: // Read the first email in the inbox only.
72: // you can change this section to read all emails.
73: for (int i = 0; i < 1; i++)
74: {
75: AtomEntry email = entries[i];
76: Console.WriteLine("Brief details of the first Email in the inbox:");
77: Console.WriteLine("Title: " + email.Title.Content);
78: Console.WriteLine("Author Name: " + email.Author.Name);
79: Console.WriteLine("Author Email: " + email.Author.Email);
80: Console.WriteLine(Environment.NewLine);
81: Console.WriteLine("Email Content: " + Environment.NewLine);
82: Console.WriteLine(email.Summary.Content);
83: Console.WriteLine(Environment.NewLine);
84: Console.WriteLine("Hit ‘Enter’ to exit");
85: Console.ReadLine();
86: }
87: }
88: else
89: {
90: Console.WriteLine("No emails found in the inbox");
91: Console.WriteLine(Environment.NewLine);
92: Console.WriteLine("Hit ‘Enter’ to exit");
93: Console.ReadLine();
94: }
95: }
96: catch (Exception ex)
97: {
98: // Note: Catching all exceptions as generic exceptions just for demo purposes.
99: // You can be more specific with handled errors
100:
101: Console.Clear();
102: ShowAppTitle();
103: Console.WriteLine("Oops! An error occurred: " + ex.Message);
104: Console.WriteLine(Environment.NewLine);
105: Console.WriteLine(Environment.NewLine);
106: Console.WriteLine("Hit ‘Enter’ to exit");
107: Console.ReadLine();
108: }
109: }
110:
111: private static void ShowAppTitle()
112: {
113: Console.WriteLine("——————————————–");
114: Console.WriteLine("Test Application that will check GMail email");
115: Console.WriteLine("Written By: Greg Olsen, 2nd Sept 2007 ");
116: Console.WriteLine("Blog: http://yellowduckguy.spaces.live.com ");
117: Console.WriteLine("——————————————–");
118: Console.WriteLine(Environment.NewLine);
119: }
120: }
121: }
Yellow Duck Guy
Greg Olsen
Hey man, this is pretty cool stuff… I was going to try this out with bloglines, but if you try to subscribe to your gmail account with a blog aggregator site like bloglines, your email will become searchable by anyone using the aggregator.See here: http://radio.weblogs.com/0140770/2005/05/14.html#a138Not sure if it\’s been fixed or not. Thought you might be interested.
hey man. I thought this was cool so I was trying to run this code from the zipped solution you provided and after submitting the username and password to the console and exception would be thrown saying:+ $exception {"The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar."} System.Exception {System.FormatException}This happened on the call in line 58.
Hey Greg, cool stuff……..i used your .cs file it helped me very much to read bounced mails. But, i want to know one thing i.e., how to get the recipient name( to whom we have sent the mail).please let me know is there any thing to be changed in code.Thanks in advance