Creating new user with Smack on ejabberd throws XMPP Exception: forbidden(403)

Goto C:\Program Files (x86)\ejabberd-2.1.8\conf (in my case) folder & open ejabberd.cfg file using Notepad++ (it is easy to edit using it).

In the file do the following changes:

%% Put this in the section ACCESS RULES
{access, register_from, [{allow, admin}]}.

%% Change mod_register so it contains the new access rule:

{mod_register, [
          {access_from, register_from},
           ...
                    ] ...

I want to update the answer to reflect the change in Asmack library version 4.0 onward. Connection.getAccountManager() is now AccountManager.getInstance(XMPPConnection)

AccountManager accountManager=AccountManager.getInstance(connection);
try {
    accountManager.createAccount("username", "password");
} catch (XMPPException e1) {
    Log.d(e1.getMessage(), e1);
}