The Facebook API doesn't allow you directly to send messages. Except that it does.

So how to do this? While looking for xmpp-libraries, I found smack. So I figured, I'd try it out:


And a class to test it out:

public class FBChatClient {
    public static final String XMPP_HOST = "";
    public static final int    XMPP_PORT = 5222;
    private final String appSecret;
    private final String appKey;

    public FBChatClient(String appKey, String appSecret) {
        this.appKey = appKey;
        this.appSecret = appSecret;

    public void login(String username, String password) throws XMPPException {
        SASLAuthentication.registerSASLMechanism("DIGEST-MD5", SASLDigestMD5Mechanism.class);
        SASLAuthentication.supportSASLMechanism("DIGEST-MD5", 0);

        ConnectionConfiguration config = new ConnectionConfiguration(XMPP_HOST, XMPP_PORT);
        XMPPConnection connection = new XMPPConnection(config);
        connection.login(username, password);

Now let's test it:

public class FBChatClientTest {

    private static final String apiKey    = ("13112999370xxxx");
    private static final String apiSecret = ("88356495a784f406efd74xxxxx");
    private FBChatClient client;

    public void setUp() throws Exception {
        this.client = new FBChatClient(apiKey, apiKey);

    public void testLogin() throws Exception {
        client.login(System.getenv("FB_USER"), System.getenv("FB_PASS"));

results in

Error! A startup class specified in smack-config.xml could not be loaded: org.jivesoftware.smackx.ServiceDiscoveryManager
Error! A startup class specified in smack-config.xml could not be loaded: org.jivesoftware.smackx.XHTMLManager

Adding smackx solved this:


But now we get:

XMPPError connecting to remote-server-error(502) XMPPError connecting to
  -- caused by: Operation timed out
    at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(
    at org.jivesoftware.smack.XMPPConnection.connect(

Turn's out, my company is blocking access on this port. So let's setup a proxy:

$ ssh -ND 8888 arc2

Now we got to change the Connection Configuration through:

ConnectionConfiguration config = new ConnectionConfiguration(XMPP_HOST, XMPP_PORT, ProxyInfo.forSocks4Proxy("localhost", 8888, null, null));

Of course, if all would work well would would let it be injected. Now let's check if that changed something:

Feb 21, 2013 11:07:31 AM login
DEBUG: connecting
Feb 21, 2013 11:07:32 AM login
DEBUG: logging in
SASL authentication DIGEST-MD5 failed: not-authorized: 
    at org.jivesoftware.smack.SASLAuthentication.authenticate(
    at org.jivesoftware.smack.XMPPConnection.login(
    at org.jivesoftware.smack.Connection.login(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(
    at org.junit.internal.runners.statements.RunBefores.evaluate(
    at org.junit.runners.ParentRunner.runLeaf(
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(
    at org.junit.runners.ParentRunner$
    at org.junit.runners.ParentRunner$1.schedule(
    at org.junit.runners.ParentRunner.runChildren(
    at org.junit.runners.ParentRunner.access$000(
    at org.junit.runners.ParentRunner$2.evaluate(
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(
    at com.intellij.rt.execution.junit.JUnitStarter.main(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at com.intellij.rt.execution.application.AppMain.main(

Now that looks better. However, it seems that we do something wrong with the authentication. Switching to CustomSASLDigestMD5Mechanism (from here) worked. If it didn't, ensure that you got the xmpp permission and check your username. You need to use the FB username (id would work too, I guess), not your email address.

Now to conclude this post, let's send a message:

XMPPConnection connection = new XMPPConnection(config);
LOG.debug("logging in");
connection.login(username, password);

Collection<RosterEntry> entries = connection.getRoster().getEntries();

Chat chat = connection.getChatManager().createChat("", null);
Message message = new Message("",;
message.setSubject("Subject cool");
message.setBody("Hier mal ein Link:");

Pay attention that you prefix the facebook id you want to send to with -. Now this is all still far from ideal since you require the user to enter his password. However, that's as far as we got. We switched platform after that so this code was not needed anymore. But maybe it can help you?