Build a Simple Gmail Notifier using Windows Forms
Google does not have an official Gmail API. Having one would have made it very easy to create a notification application. In this article, I will show you how to exploit the Feed facility of Gmail to build our own Gmail Notifier.
Gmail Feed is an useful feature provided by Gmail using which we can subscribe to emails using a Feed reader supporting authentication. You have feeds for your unread mails as well as feeds for your labels.
Before starting to build the application, let’s see how our final product will look like. Below are the screenshots of Login and Notifier screens.
Let’s get started.
Step 1: Fire up Visual Studio 2008 and Create a new Windows Application by going to File > New Project > Windows > Windows Form Application
Step 2: Add a new form ‘Login form’ to the project by Right Click > Add > New Item. Add 2 textboxes and 1 Button control to the Form. Name the Textboxes as ‘UsernameTextBox’ and ‘PasswordTextBox’ respectively.
Step 3: Now go back to Form1 (default form which was added when you created this project) and add 2 labels and a Timer control and name the 2 labels as ‘lblFrom’ and ‘lblMessage’respectively.
Step 4: Add a Class file to the project, to declare Global Variables used in the project using Right Click -> Add -> New Item
Add the following code to the class file:
internal static class GlobalVariables
public static string emailFrom = new string;
public static string emailMessages = new string;
public static Int16 tempCounter = 0;
public static Int16 mailCount = 0;
Public emailFrom(1), emailMessages(1) As String
Public tempCounter As Int16 = 0
Public mailCount As Int16 = 0
Step 5: Double Click the ‘OK’ button in the Login form and type in the following code to the Click event
Public Class LoginForm1
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Dim objClient As New System.Net.WebClient
Dim nodelist As XmlNodeList
Dim node As XmlNode
Dim response As String
Dim xmlDoc As New XmlDocument
objClient.Credentials = New System.Net.NetworkCredential(UsernameTextBox.Text.Trim, PasswordTextBox.Text.Trim)
response = Encoding.UTF8.GetString(objClient.DownloadData("https://mail.google.com/mail/feed/atom"))
response = response.Replace("<feed version=""0.3"" xmlns=""http://purl.org/atom/ns#"">", "<feed>")
node = xmlDoc.SelectSingleNode("/feed/fullcount")
mailCount = node.InnerText 'Get the number of unread emails
If mailCount > 0 Then
ReDim emailFrom(mailCount - 1)
ReDim emailMessages(mailCount - 1)
nodelist = xmlDoc.SelectNodes("/feed/entry")
node = xmlDoc.SelectSingleNode("title")
For Each node In nodelist
emailMessages(tempCounter) = node.ChildNodes.Item(0).InnerText
emailFrom(tempCounter) = node.ChildNodes.Item(6).ChildNodes(0).InnerText
tempCounter += 1
tempCounter = 0
Catch ex As Exception
Explanation : In the code shown above, we first create a new object of ‘Webclient’ class and then request feed from Gmail using the Username and Password as the network credentials. ‘Gmail Feed’ is an useful feature provided by Gmail using which we can subscribe to emails using a Feed reader supporting authentication.
Gmail feed is an ATOM feed. Here is an article from MSDN about how we can parse ATOM feeds. But to make our job easier we remove the text <feed version="0.3"mlns="http://purl.org/atom/ns#"> which will help us to parse the response as a normal XML file using ‘XmlDocument’
Once the XML message is received, we use the /feed/fullcount to check for any new emails and assign the value to ‘mailCount’. If this value is greater than 0, we store the unread message details in an array.
Step 6: Now open the Form1 ‘Load’ event and add the folowing code which will position the window at the bottom right corner as shown in the screenshot at the beginning of the article.
private void Form1_Load(object sender, System.EventArgs e)
Timer1.Enabled = true;
this.ShowInTaskbar = false;
Left = (SystemInformation.WorkingArea.Size.Width - Size.Width);
Top = (SystemInformation.WorkingArea.Size.Height - Size.Height);
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Me.ShowInTaskbar = False
Left = (SystemInformation.WorkingArea.Size.Width - Size.Width)
Top = (SystemInformation.WorkingArea.Size.Height - Size.Height)
Step 7: Now set the Timer’s interval to 2000 (2 seconds) and add the following code to the ‘Tick’ event.
private void Timer1_Tick(object sender, System.EventArgs e)
if (tempCounter >= mailCount)
Timer1.Enabled = false;
lblFrom.Text = "From : " + emailFrom(tempCounter);
lblMessage.Text = "Subject : " + emailMessages(tempCounter);
tempCounter += 1;
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If tempCounter >= mailCount Then
Timer1.Enabled = False
lblFrom.Text = "From : " & emailFrom(tempCounter)
lblMessage.Text = "Subject : " & emailMessages(tempCounter)
tempCounter += 1
That’s it. Now run the project and see your Gmail Notifier in action. The source code of this article can be downloaded from here.