Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package util;
- import org.jivesoftware.smack.*;
- import org.jivesoftware.smack.packet.Message;
- import java.util.Calendar;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.text.SimpleDateFormat;
- /**
- * Created by IntelliJ IDEA.
- * User: Gautam
- * Date: Dec 16, 2009
- * Time: 11:32:23 AM
- * To change this template use File | Settings | File Templates.
- */
- public class gchat {
- static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
- static HashMap hp = new HashMap();
- static ConnectionConfiguration config;
- static XMPPConnection connection;
- static Chat chat;
- static String user = "";
- public static String now() {
- Calendar cal = Calendar.getInstance();
- SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
- return sdf.format(cal.getTime());
- }
- public static void setup() throws XMPPException {
- hp.put("ToUser@gmail.com", "");
- //IP of talk.google.com
- config = new ConnectionConfiguration("64.233.169.125", 5222, "gmail.com");
- connection = new XMPPConnection(config);
- connection.connect();
- connection.login("FromUser@gmail.com", "Password");
- }
- public void disconnect() {
- connection.disconnect();
- }
- public static void sendMessage(String messages) throws Exception {
- if (hp.isEmpty())
- setup();
- try {
- Iterator it = hp.keySet().iterator();
- while (it.hasNext()) {
- user = it.next().toString();
- chat = connection.getChatManager().createChat(user, new MessageListener() {
- public void processMessage(Chat chat, Message message) {
- System.out.println("Received message: " + message);
- }
- });
- chat.sendMessage("Automated message from Gautam's robot at: " + now() + ". Message: " + messages);
- }
- }
- catch (XMPPException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement