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:
C#
internal static class GlobalVariables
{
public static string[] emailFrom = new string[2];
public static string[] emailMessages = new string[2];
public static Int16 tempCounter = 0;
public static Int16 mailCount = 0;
}
VB.NET
Module GlobalVariables
Public emailFrom(1), emailMessages(1) As String
Public tempCounter As Int16 = 0
Public mailCount As Int16 = 0
End Module
Step 5: Double Click the ‘OK’ button in the Login form and type in the following code to the Click event
VB.NET
Imports System.Xml
Imports System.Text
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
Try
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>")
xmlDoc.LoadXml(response)
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
Next
tempCounter = 0
End If
Me.Hide()
Form1.Show()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
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.
C#
private void Form1_Load(object sender, System.EventArgs e)
{
Timer1.Enabled = true;
this.Hide();
this.ShowInTaskbar = false;
Left = (SystemInformation.WorkingArea.Size.Width - Size.Width);
Top = (SystemInformation.WorkingArea.Size.Height - Size.Height);
}
VB.NET
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Me.Hide()
Me.ShowInTaskbar = False
Left = (SystemInformation.WorkingArea.Size.Width - Size.Width)
Top = (SystemInformation.WorkingArea.Size.Height - Size.Height)
End Sub
Step 7: Now set the Timer’s interval to 2000 (2 seconds) and add the following code to the ‘Tick’ event.
C#
private void Timer1_Tick(object sender, System.EventArgs e)
{
this.Show();
if (tempCounter >= mailCount)
{
Timer1.Enabled = false;
this.Hide();
}
else
{
lblFrom.Text = "From : " + emailFrom(tempCounter);
lblMessage.Text = "Subject : " + emailMessages(tempCounter);
tempCounter += 1;
}
}
VB.NET
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Show()
If tempCounter >= mailCount Then
Timer1.Enabled = False
Me.Hide()
Else
lblFrom.Text = "From : " & emailFrom(tempCounter)
lblMessage.Text = "Subject : " & emailMessages(tempCounter)
tempCounter += 1
End If
End Sub
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.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
ShobanKumar is an ex-Microsoft MVP in SharePoint who currently works as a SharePoint Consultant. You can read more about his projects at
http://shobankumar.com. You can also follow him on twitter @
shobankr