· 10 years ago · Nov 18, 2015, 02:12 PM
1package util;
2
3import org.jivesoftware.smack.*;
4import org.jivesoftware.smack.packet.Message;
5
6import java.util.Calendar;
7import java.util.HashMap;
8import java.util.Iterator;
9import java.text.SimpleDateFormat;
10
11/**
12* Created by IntelliJ IDEA.
13* User: Gautam
14* Date: Dec 16, 2009
15* Time: 11:32:23 AM
16* To change this template use File | Settings | File Templates.
17*/
18public class gchat {
19
20static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
21static HashMap hp = new HashMap();
22static ConnectionConfiguration config;
23static XMPPConnection connection;
24static Chat chat;
25static String user = "";
26
27public static String now() {
28Calendar cal = Calendar.getInstance();
29SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
30return sdf.format(cal.getTime());
31}
32
33public static void setup() throws XMPPException {
34hp.put("ToUser@gmail.com", "");
35//IP of talk.google.com
36config = new ConnectionConfiguration("64.233.169.125", 5222, "gmail.com");
37connection = new XMPPConnection(config);
38connection.connect();
39connection.login("FromUser@gmail.com", "Password");
40}
41
42public void disconnect() {
43connection.disconnect();
44}
45
46public static void sendMessage(String messages) throws Exception {
47if (hp.isEmpty())
48setup();
49try {
50Iterator it = hp.keySet().iterator();
51while (it.hasNext()) {
52user = it.next().toString();
53chat = connection.getChatManager().createChat(user, new MessageListener() {
54public void processMessage(Chat chat, Message message) {
55System.out.println("Received message: " + message);
56}
57});
58chat.sendMessage("Automated message from Gautam's robot at: " + now() + ". Message: " + messages);
59}
60}
61catch (XMPPException e) {
62e.printStackTrace();
63}
64}
65}