Category: "XMPP"

Smack version 3.1.0 and GMail server

October 19th, 2010

Currently I’m working on my Master’s Dissertation, and one of my needs is to connect a XMPP server, GMail for instance.

To do this, I was using Smack version 3.0.0 and everything was going well, even connecting GMail, but… when I did update to version 3.1.0 it started to show an f@&*$!* error message: SASL authentication failed using mechanism PLAIN
It took a while (at least to me ), but I was able to figure out this solution. Note: I’ve googled a lot without success ;-)

So, bellow is the working code, hope it helps you.

Cheers,
Vilson C. Gärtner
http://twitter.com/VilsonGartner

Code

public static void main(String[] args)
    {
 
        System.out.println("Starting...");
 
        ConnectionConfiguration connCfg = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
 
        connCfg.setSASLAuthenticationEnabled(false);
        XMPPConnection connection = new XMPPConnection(connCfg);
 
        System.out.println("Connecting...");
        try
        {
            connection.connect();
            System.out.println("Connected to " + connection.getHost());
        }
        catch (XMPPException ex)
        {
            System.out.println("Failed to connect to " + connection.getHost() + ": " + ex.getMessage());
            System.exit(1);
        }
 
        System.out.println("Loggin in...");
 
        try
        {
            connection.login("userName", "password");
            System.out.println("Logged in as " + connection.getUser());
 
            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
 
        }
        catch (XMPPException ex)
        {
            System.out.println("Failed to log in as " + connection.getUser() + ": " + ex.getMessage());
            System.exit(1);
        }
 
        Roster roster = connection.getRoster();
        Collection<RosterEntry> entries = roster.getEntries();
 
        System.out.println("\n\n" + entries.size() + " buddy(ies):");
        for (RosterEntry r : entries)
        {
            System.out.println(r.getUser());
        }
 
        connection.disconnect();
    }