Home > Mail Server > Using Apache JAMES Mail Server

Using Apache JAMES Mail Server

Recently, I needed an open source email server for testing purposes and I came across JAMES project. The Java Apache Mail Enterprise Server or JAMES is an open source mail application platform that among many other things is a full-fledged e-mail server. It is a pure Java implementation (built on Spring Framework) and supports all the standard e-mail protocols such as SMTP, POP3 etc. In this post, I will quickly demonstrate how you can set up JAMES on your local Windows box and use it to send emails.

Step 1: Download the latest version of JAMES from Apache site. At the time of this writing the latest stable version is 2.3.2 (james-binary-2.3.2.zip). Unzip the downloaded file to a folder on your box. I will be referring to this directory as JAMES_INSTALL_DIR throughout post.

Step 2: To start JAMES, simply run the “run.bat” file under the \bin. This should open up a new window that looks something like this:

Step 3: Before we can start sending and receiving mails, we need to create a user account on our mail server. JAMES provides a Remote Manager Service on port 4555 that you can telnet into and interact with. Simply launch a new Command Prompt and run the command:
telnet localhost 4555

You should see the Remote Administration Tool and a prompt for your login id.

Enter root as your login id and upon prompt for password enter root. This would log you in. To create a new user run the following command: adduser someusername somepassword

Here are some of the useful commands supported by JAMES:

Command Description
help Display help with commands
listusers
setpassword Sets a user’s password. The format is setpassword username password
deluser Deletes an existing user. The format is deluser username
adduser Adds a new user. The format is adduser username password
countusers Shows the number of existing accounts
listusers Lists all the existing accounts in the mail server
quit Closes the connection

Step 4: Now we have everything installed and setup to send emails. Lets give it a try by running the sample code below:


package com.practicalspring.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Main {
	
	public static void main(String[] args) 
	{
		/***** CHANGE THESE FOUR VARIABLE VALUES TO REFLECT YOUR ENVIRONMENT ******/
		String user = "vader";	// Newly created user on JAMES
		String password = "darth"; // user password
		
		String fromAddress = "vader@localhost"; // newlycreateduser@localhost 
		String toAddress = "SOMEUSER@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("Luke, I'm your father!!");
			Transport.send(message);
			
			System.out.println("Email sent successfully");
		}
		catch (MessagingException e) 
		{
			e.printStackTrace();
		}
	}
}

Here is the generated email in my inbox:

If you don’t see an email in your inbox in the next couple minutes make sure to check in the Spam folder.

Categories: Mail Server Tags: ,
  1. sumeet
    March 20th, 2011 at 11:12 | #1

    when i run your program i get following error with
    i have made appropriate changes like username,etc
    Exception in thread “main” com.sun.mail.smtp.SMTPSendFailedException: 550 The address is not valid.
    ;
    nested exception is:
    com.sun.mail.smtp.SMTPSenderFailedException: 550 The address is not valid.

  2. jason
    April 26th, 2011 at 07:24 | #2

    This works perfectly as long as I send and receive from localhost. Sending to gmail doesn’t work, but that’s probably an IT security thing. Thanks for the awesome tutorial!

  3. mohit jain
    May 29th, 2011 at 06:13 | #3

    How can i add a new user to james from a servlet? please help.

  4. nikki
    August 22nd, 2011 at 15:51 | #4

    hey man i m not getting any mail using this program…!!!please help me out..!!!i had done program of getting email in intranet using james server but when i sent to outside world mail is not reaching..!!!

  5. Ashis
    February 29th, 2012 at 10:54 | #5

    First of all thank you. I also got the same problem, while sending email to external network. Please give the solution to send email to external network(e.g gmail or yahoo).

  6. sumithra
    March 9th, 2012 at 11:35 | #6

    how to execute the above code?i am from a ece background i don’t know how to execute it??is it similar to java execution like javac filename.java

  7. surya
    May 14th, 2012 at 06:52 | #7

    Hi i am used the above code,
    i am getting the sop statement without any error mesg, but the mail is not appearing in inbox/spam……

    please kindly reply me

  8. manas sahoo
    July 5th, 2012 at 03:24 | #8

    Thanks a lot .
    It really helped me a lot . I was searching for this . U really did awesome job. Carry on …

  9. Pavan
    August 2nd, 2012 at 06:33 | #9

    Hi All,

    Am working on James Server,
    I have written a custom mailet for mail delivery and added the mailet in config.xml.
    when i sent any mail to users, am getting the delivery notification.
    am trying to send a a mail which a non exist user,
    this suppose to get a bounce back mail.
    am not getting that mail.

    please help in this.

    Thanks InAdvance.
    Pavan

  10. bala
    February 6th, 2013 at 05:42 | #10

    @jason

    Hi,

    This very good doc for beginners. I can send the mails to internal network. But when I sending to external network like gmail,yahoo,etc… I am not getting the mails. Please help on this.

  11. Ashish
    March 5th, 2013 at 06:38 | #11

    Hi Jason,

    It’s Really very neat-and-clean example related to James Apache , But If you can also provide the configuration required for sending mail to external mail account like (gmail,yahoo,..) in james configuration file ,that will really appreciable ………….Thanks

  1. September 20th, 2011 at 03:57 | #1
  2. July 18th, 2012 at 04:21 | #2