This is a simple Google chat ( gtalk ) desktop application developed in C#. With this application, we can easily chat with our Gmail contacts. This application serves as a best approach for developers to start with the chat application for Gmail.
This application is developed using a .NET open source library, Jabber.net. This library contains a set of .NET controls for sending and receiving extensible messaging and presence protocol (XMPP), also known as the Jabber.
Using the Code
After installing the Jabber.Net library, you can see the list of Jabber.Net controls in the Visual Studio toolbox. Now, add the
JabberClient control to the form and assign the user name and password.
User = txtUserName.Text;
Pwd = txtPassword.Text;
jabberClient1.User = User;
jabberClient1.Server = "gmail.com";
jabberClient1.Password = Pwd;
jabberClient1.AutoRoster = true;
Now, add a
RosterTree control to populate the contact list and assign the instance of the roster manager and the presence manager to the roster tree control.
rm = new RosterManager();
rm.Stream = jabberClient1;
rm.AutoSubscribe = true;
rm.AutoAllow = jabber.client.AutoSubscriptionHanding.AllowAll;
rm.OnRosterBegin += new bedrock.ObjectHandler(rm_OnRosterBegin);
rm.OnRosterEnd += new bedrock.ObjectHandler(rm_OnRosterEnd);
rm.OnRosterItem += new RosterItemHandler(rm_OnRosterItem);
RosterManager will add the contact list to the roster tree and the
PresenceManager will add a notification for the contact whether the contact is in online or offline mode.
pm = new PresenceManager();
pm.Stream = jabberClient1;
rosterTree1.RosterManager = rm;
rosterTree1.PresenceManager = pm;
After assigning all of the above, we have to call the
connect() method to login with the given credentials and populate the roster tree with the contact list.
jabberClient1.OnAuthenticate += new bedrock.ObjectHandler(jabberClient1_OnAuthenticate);
On double clicking a contact in the contact list will open a chat window, through which the user can send messages using the following function:
private void SendMessage()
jabber.protocol.client.Message reply =
if (rtbSendMessage.Text != "\n" && rtbSendMessage.Text != " ")
reply.Body = rtbSendMessage.Text;
if (reply.Body != "")
reply.To = _mailId;
string sentMsg = _jabberClient.User + " Says : " +
rtbSendMessage.Text + "\n";
rtbSendMessage.Text = "";
JabberClient is registered with an
onMessage event to receive whenever an incoming message arrives. This is handled by the following function:
public void _jabberClient_OnMessage
(object sender, jabber.protocol.client.Message msg)
if (msg.From.Bare == this.MailId)
if (msg.Body != "")
string receivedMsg = msg.From.User + " Says : " + msg.Body + "\n";
msg.Body = "";