Pages Navigation Menu

Coding is much easier than you think

Sending chat to Google Talk in Java



In this post , we will see that how to integrate Google Talk(Most popular chat client) with Java Code.
Follow these steps to integrate Google talk with Java
 
Before start please download the following library and add it in your class path
 

Note:-This code will only send chat, it is not able to receive msg

 
dwd2
Download It – Smack 3_2_2

 

package com.simplecode.chat;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.ConnectionConfiguration;

public class GtalkChat {
	
	private static String username = "[email protected]";
	private static String password = "XXXXXXX";
	ConnectionConfiguration connConfig;
	XMPPConnection connection;

	public GtalkChat() throws XMPPException
        {
	connConfig = new ConnectionConfiguration("talk.google.com", 5222,"gmail.com");
	connection = new XMPPConnection(connConfig);
	connection.connect();
	connection.login(username, password);
	}

	public void sendMessage(String to, String message)
        {
		Message msg = new Message(to, Message.Type.chat);
		msg.setBody(message);
		connection.sendPacket(msg);
	}

	public void disconnect()
        {
		connection.disconnect();
	}

	public static void main(String[] args) throws XMPPException
        {
		GtalkChat gtalkChat = new GtalkChat();
		gtalkChat.sendMessage("[email protected]",
		"Hai this message is send using Java with Google talk integration :P ");
		gtalkChat.disconnect();
	}
}

 

About Jamil


Am currently working as a Struts 2, Webservices Developer in a reputed IT Organisations. I can help you with teaching Core java , Struts 2, Webservices and Site Design.

    How to delete file if exists in java?
    Resetting a form on page load
    SimpleCodeStuffs located at 511/67 Huynh Van Banh , Ho Chi Minh, VN . Reviewed by 56 customers rated: 4 / 5