title: Cannot connect simple Java Smack example to Google Talk
body: <p>I have followed a series of tutorials and have come up with a very simple and minimalistic Java app that should connect to google talk and allow me to send message to an account.</p>
  
  <p>I have turned off the SASLAuth security settings in order to make the example work, however when I try to connect to google servers, I get a complex TLS error and I think  that I am forced to enable the SASLAuth security settings. However, if I enable them, I get yet another error. Here are very simple code samples I am using. What am I missing?</p>
  
  <p><strong>XmppManager.java</strong>:</p>
  
  <pre><code>package try2;
  
  import java.util.Collection;
  
  import org.jivesoftware.smack.Chat;
  import org.jivesoftware.smack.ChatManager;
  import org.jivesoftware.smack.ConnectionConfiguration;
  import org.jivesoftware.smack.MessageListener;
  import org.jivesoftware.smack.Roster;
  import org.jivesoftware.smack.RosterEntry;
  import org.jivesoftware.smack.SmackConfiguration;
  import org.jivesoftware.smack.XMPPConnection;
  import org.jivesoftware.smack.XMPPException;
  import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
  import org.jivesoftware.smack.packet.Message;
  import org.jivesoftware.smack.packet.Presence;
  import org.jivesoftware.smack.packet.Presence.Type;
  
  public class XmppManager {
  
      private static final int packetReplyTimeout = 500; // millis
  
      private String server;
      private int port;
      private String service;
  
      private ConnectionConfiguration config;
      private XMPPConnection connection;
  
      private ChatManager chatManager;
      private MessageListener messageListener;
  
      public XmppManager(String aServer, int aPort, String aService) {
          server = aServer;
          port = aPort;
          service = aService;
      }
  
      public XmppManager(String server, int port) {
          this(server, port, null);
      }
  
      public void init() throws XMPPException {
  
          System.out.println(String.format("Initializing connection to server " +  server + ", port " + port 
                  + ", service " + service));
  
          SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);
  
          if(service != null)
              config = new ConnectionConfiguration(server, port, service);
          else
              config = new ConnectionConfiguration(server, port);
  
          config.setSASLAuthenticationEnabled(false);
          config.setSecurityMode(SecurityMode.disabled);
  
          connection = new XMPPConnection(config);
          connection.connect();
  
          System.out.println("Connected: " + connection.isConnected());
  
          chatManager = connection.getChatManager();
          messageListener = new MyMessageListener();
  
      }
  
      public void performLogin(String username, String password) throws XMPPException {
          if (connection!=null &amp;&amp; connection.isConnected()) {
              connection.login(username, password);
          }
      }
  
      public void setStatus(boolean available, String status) {
  
          Presence.Type type = available? Type.available: Type.unavailable;
          Presence presence = new Presence(type);
  
          presence.setStatus(status);
          connection.sendPacket(presence);
  
      }
  
      public void destroy() {
          if (connection!=null &amp;&amp; connection.isConnected()) {
              connection.disconnect();
          }
      }
  
      public void sendMessage(String message, String buddyJID) throws XMPPException {
          System.out.println(String.format("Sending message " + message + " to user " + buddyJID));
          Chat chat = chatManager.createChat(buddyJID, messageListener);
          chat.sendMessage(message);
      }
  
      public void createEntry(String user, String name) throws Exception {
          System.out.println(String.format("Creating entry for buddy " + user + " with name " + name));
          Roster roster = connection.getRoster();
          roster.createEntry(user, name, null);
      }
  
      public void printRoster() throws Exception {
          Roster roster = connection.getRoster();
          Collection&lt;RosterEntry&gt; entries = roster.getEntries();  
          for (RosterEntry entry : entries) {
              System.out.println(String.format("Buddy:" + entry.getName() + " - Status:" + entry.getStatus()));
          }
      }
  
  
      private class MyMessageListener implements MessageListener {
  
          @Override
          public void processMessage(Chat chat, Message message) {
              String from = message.getFrom();
              String body = message.getBody();
              System.out.println(String.format("Received message " + body + " from " + from));
          }
  
      }
  
  }
  </code></pre>
  
  <p><strong>XmppTest.java</strong>:</p>
  
  <pre><code>package try2;
  
  public class XmppTest {
  
      public static void main(String[] args) throws Exception {
  
          String username = "user1";
          String password = "user1.pass";
  
          XmppManager xmppManager = new XmppManager("talk.google.com", 5222, "gmail.com");
  
          xmppManager.init();
          xmppManager.performLogin(username, password);
          xmppManager.setStatus(true, "Hello everyone");
  
          String buddyJID = "user2";
          String buddyName = "user2";
          xmppManager.createEntry(buddyJID, buddyName);
  
          xmppManager.sendMessage("Hello mate", "user2@gmail.com");
  
          xmppManager.printRoster();
  
          boolean isRunning = true;
  
          while (isRunning) {
              Thread.sleep(50);
          }
  
          xmppManager.destroy();
  
      }
  
  }
  </code></pre>
  
  <p>I think the error is related to these two code lines:</p>
  
  <pre><code>config.setSASLAuthenticationEnabled(false);
  config.setSecurityMode(SecurityMode.disabled);
  </code></pre>
  
  <p>However if it is, then I have no idea how to configure them correctly !</p>
  
  <p><strong>Error with SASLAuth disabled</strong>:</p>
  
  <pre><code>Initializing connection to server talk.google.com, port 5222, service gmail.com
  java.lang.IllegalStateException: TLS required by server but not allowed by connection configuration
      at org.jivesoftware.smack.XMPPConnection.startTLSReceived(XMPPConnection.java:768)
      at org.jivesoftware.smack.PacketReader.parseFeatures(PacketReader.java:414)
      at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:254)
      at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:46)
      at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:72)
  Connected: true
  Exception in thread "main" No response from the server.: 
      at org.jivesoftware.smack.NonSASLAuthentication.authenticate(NonSASLAuthentication.java:73)
      at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:252)
      at org.jivesoftware.smack.Connection.login(Connection.java:366)
      at try2.XmppManager.performLogin(XmppManager.java:70)
      at try2.XmppTest.main(XmppTest.java:13)
  </code></pre>
  
  <p><strong>Error with SASLAuth enabled</strong>:</p>
  
  <pre><code>Exception in thread "main" SASL authentication PLAIN failed: invalid-authzid: 
      at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:342)
      at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:243)
      at org.jivesoftware.smack.Connection.login(Connection.java:366)
      at try2.XmppManager.performLogin(XmppManager.java:70)
      at try2.XmppTest.main(XmppTest.java:13)
  </code></pre>