Sending an Email from Java Using JavaMail and Apache James

Email! Get connected!

The phrase “Email! Get connected!” is a registered trademark of
This Could Be Better LLC, which is a company that doesn’t exist,
and also it’s not a registered trademark because no one ever, um,
registered it. All rights reserved. Patent pending. Code provided
as is. Which isn’t a sentence. And neither was that. Or that, or
this. Offer void in Canada.

1. If you have not already done so, download and install the Java Development Kit. Details are available in a previous tutorial. Make a note of the directory to which the files “javac.exe” and “java.exe” are installed.

2. Download the Apache James mail server. As of this writing, the latest stable version is available at the URL “http://james.apache.org/server/”. Locate and download the “Binary ZIP” format of Apache James version 2.3.2.

3. Extract the downloaded ZIP archive for Apache James to any convenient directory. Open the extracted directory, then the “bin” subdirectory, and locate the file “run.bat”. Double click run.bat to start the default mail server in a console window. This console window should remain open for the rest of the tutorial.

4. In any convenient location, create a new directory called “EmailTest”.

5. Download the JavaMail library. JavaMail is Java’s official email library. As of this writing, the latest version is available at “http://www.oracle.com/technetwork/java/javamail-138606.html”.

6. Extract the downloaded ZIP archive for JavaMail to any convenient location. Open the extracted directory, and copy the precompiled library file “mail.jar” to the EmailTest directory.

7. In the EmailTest directory, create a new text file named “EmailTest.java”, containing the following text.

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class EmailTest
{
    public static void main(String[] args)
    {
        String addressee = args[0];        

        EmailMessage emailMessage = new EmailMessage
        (
            "nonesuch@bogusiosity.org",
            addressee,
            "test email",
            "This is a test email."
        );

        emailMessage.send();

        System.out.println("email message sent to " + addressee);
    }
}

class EmailMessage
{
    public String sender;
    public String addressee;
    public String subject;
    public String body;

    public EmailMessage
    (
        String sender,
        String addressee,
        String subject,
        String body
    )
    {
        this.sender = sender;
        this.addressee = addressee;
        this.subject = subject;
        this.body = body;
    }

    public void send()
    {
        Properties mailServerProperties = new Properties();
        mailServerProperties.put
        (
            "mail.smtp.host",
            "localhost"
        );
        mailServerProperties.put
        (
            "mail.smtp.port",
            "25"
        );

        Session session = Session.getDefaultInstance
        (
            mailServerProperties
        );

        MimeMessage messageToSend = new MimeMessage(session);

        try
        {
            messageToSend.setFrom
            (
                new InternetAddress(this.sender)
            );
            messageToSend.addRecipient
            (
                Message.RecipientType.TO,
                new InternetAddress(this.addressee)
            );
            messageToSend.setSubject(this.subject);
            messageToSend.setText(this.body);

            Transport.send(messageToSend);
        }
        catch (MessagingException ex)
        {
            ex.printStackTrace();
        }
    }

}

8. Still in the EmailTest directory, create a new text file named “ProgramBuildAndRun.bat”, containing the following text. Substitute the path of the directory containing javac.exe and a test email address in the indicated places.

set javaPath="[the path of the directory containing javac.exe]"

@echo on
%javaPath%\javac.exe -classpath .;mail.jar *.java
%javaPath%\java.exe -classpath .;mail.jar EmailTest [a test email address]

pause

9. Execute ProgramBuildAndRun.bat. The test program will be compiled and run, and an email will be sent to the email account specified.

10. Check the specified email account and verify that the test email has been received.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

2 Responses to Sending an Email from Java Using JavaMail and Apache James

  1. Mihir says:

    I tried above code . It works but actual email never sent to intended recipient . Why ?

  2. For configuration visit : http://www.mobilefish.com/developer/james/james.html
    I tried this and found that send mail and read mail functionality is working to send and receive mails from the user who are in apache james server only (for localhost only).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s