0

I need add new users in Apache James mail server using Java API, but in the whole internet there is not any example of how to use it. Only this http://james.apache.org/server/2.3.1/adding_users.html with Java mail API you can send mail to Apache James mailbox read messages, but only that.

String user = "user";  // Newly created user on JAMES  
String password = "user"; // user password  

String fromAddress = "user@localhost"; // newlycreateduser@localhost   
String toAddress = "usver@gmail.com";


// Create a mail session  
Properties properties = new Properties();
properties.put("mail.smtp.host", "localhost");
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.username", user);
properties.put("mail.smtp.password", password);
Session session = Session.getDefaultInstance(properties, null);

try
{
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(fromAddress));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));

    message.setSubject("Email from our JAMES Server");
    message.setText("Hello world again and again");
    Transport.send(message);

    System.out.println("Email sent successfully");
}
catch (MessagingException e)

{
    e.printStackTrace();
}

Anybody know or used James Java API? Any examples?

Mardoz
  • 1,617
  • 1
  • 13
  • 26
Armen Arzumanyan
  • 1,939
  • 3
  • 30
  • 56

4 Answers4

1

Here is the equivalent java code:

public static void main(String [] args){

    addUser(args[0]);
}

static void addUser(String email){
    try{
        String serverUrl = "service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi";
        String beanNameUser = "org.apache.james:type=component,name=usersrepository";
        String beanNameDomain = "org.apache.james:type=component,name=domainlist";

        MBeanServerConnection server = JMXConnectorFactory.connect(new JMXServiceURL(serverUrl)).getMBeanServerConnection();

        UsersRepositoryManagementMBean userBean =  MBeanServerInvocationHandler.newProxyInstance(server, new ObjectName(beanNameUser), UsersRepositoryManagementMBean.class, false);
        DomainListManagementMBean domainBean =  MBeanServerInvocationHandler.newProxyInstance(server, new ObjectName(beanNameDomain), DomainListManagementMBean.class, false);

        if(domainBean.containsDomain(email.split("@")[1])
                && !userBean.verifyExists(email)){
            System.out.println("creating email : "+email );
            userBean.addUser(email,"password");
        }else{
            System.out.println("domain does not exist or user already exists !!");
        }

    }catch (Exception e){
        System.out.println("Something went wrong");
    }
}
zaki benz
  • 672
  • 7
  • 21
  • I'm using same code but I'm unable connect with james server. Getting this exception. `server.java.io.IOException: Failed to retrieve RMIServer stub: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException:` – Parth Solanki Mar 27 '18 at 17:36
  • Do your James server use the same port 9999 on localhost. Serverurl may need to be adjusted. – zaki benz Mar 28 '18 at 05:20
  • `Caused by: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: java.net.ConnectException: Connection refused: connect] at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.jav1a:136) at com.sun.jndi.toolkit.url.GenericURLContext.lookup(GenericURLContext.java:205) at javax.naming.InitialContext.lookup(InitialContext.java:417) at javax.management.remote.rmi.RMIConnector.findRMIServerJNDI(RMIConnector.java:1955)` – Parth Solanki Mar 28 '18 at 12:43
  • No I mean: on which port your James is listening. – zaki benz Mar 28 '18 at 20:56
0

You may need to use Runtime.exec to call the James adduser command. If you want to do that remotely, you'll need a Java telnet or ssh client to connect to the server where James is running.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
0

I understand what I should use apache james api, build from source and use JPA functionality.

Armen Arzumanyan
  • 1,939
  • 3
  • 30
  • 56
0

Hi this module will help you in doing so as this is in groovy so Please change "def" to desired Datatye/object of a class this is just for a idea but it require effort to convert this to JAVA code.

    def addUserToJames(String email){
    try{
        def serverUrl = 'service:jmx:rmi://localhost/jndi/rmi://localhost:9999/jmxrmi'
        String beanNameUser = "org.apache.james:type=component,name=usersrepository"
        String beanNameDomain = "org.apache.james:type=component,name=domainlist"
        def server = JMXConnectorFactory.connect(new JMXServiceURL(serverUrl)).MBeanServerConnection
        def userBean = new GroovyMBean(server, beanNameUser)
        def domainBean = new GroovyMBean(server, beanNameDomain)
        if(!domainBean.containsDomain(email.split('@')[1])){
            domainBean.addDomain(email.split('@')[1])
        }
        userBean.addUser(email,'Welcome123!')
    }catch (Exception e){
        println "Something went wrong"
    }
}
Shashank.gupta40
  • 915
  • 1
  • 8
  • 26