Advertisement
Guest User

Untitled

a guest
Nov 18th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. package util;
  2.  
  3. import org.jivesoftware.smack.*;
  4. import org.jivesoftware.smack.packet.Message;
  5.  
  6. import java.util.Calendar;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import 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. */
  18. public class gchat {
  19.  
  20. static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
  21. static HashMap hp = new HashMap();
  22. static ConnectionConfiguration config;
  23. static XMPPConnection connection;
  24. static Chat chat;
  25. static String user = "";
  26.  
  27. public static String now() {
  28. Calendar cal = Calendar.getInstance();
  29. SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
  30. return sdf.format(cal.getTime());
  31. }
  32.  
  33. public static void setup() throws XMPPException {
  34. hp.put("ToUser@gmail.com", "");
  35. //IP of talk.google.com
  36. config = new ConnectionConfiguration("64.233.169.125", 5222, "gmail.com");
  37. connection = new XMPPConnection(config);
  38. connection.connect();
  39. connection.login("FromUser@gmail.com", "Password");
  40. }
  41.  
  42. public void disconnect() {
  43. connection.disconnect();
  44. }
  45.  
  46. public static void sendMessage(String messages) throws Exception {
  47. if (hp.isEmpty())
  48. setup();
  49. try {
  50. Iterator it = hp.keySet().iterator();
  51. while (it.hasNext()) {
  52. user = it.next().toString();
  53. chat = connection.getChatManager().createChat(user, new MessageListener() {
  54. public void processMessage(Chat chat, Message message) {
  55. System.out.println("Received message: " + message);
  56. }
  57. });
  58. chat.sendMessage("Automated message from Gautam's robot at: " + now() + ". Message: " + messages);
  59. }
  60. }
  61. catch (XMPPException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement