This document describes the usage of JavaMail API, It also describes the usage of property/xml file to set the JavaMail API’s environment variables. The JavaMail API is designed for sending and receiving emails. It includes the following classes, which encapsulate common mail functions and protocols.

The Message Class

It defines the set of attributes and content for a mail message.

The Mail Session

It manages the configuration options and user authentication information for communicating with a mail server.

Store Class

It models a message store and its access protocol, for storing and retrieving messages.

1.  Environment properties in JavaMail:

This section lists the environment properties that are used by the JavaMail APIs.

It specifies the default Message Access Protocol. The Session.getStore() method returns a Store object that implements this protocol.


It specifies the default Transport Protocol. The Session.getTransport() method returns a Transport object that implements this protocol.

It specifies the default Mail server.


It specifies the username to provide when connecting to a Mail server.

It specifies the protocol-specific default Mail server. This overrides the property –


It specifies the protocol-specific default username for connecting to the Mail server. This overrides the mail.user property – mail.user


It specifies the return address of the current user.


It specifies the initial debug mode. Setting this property to true will turn on debug mode, while setting it to false turns debug mode off.

Note that the Session.setDebug method also controls the debug mode – false

2.  Setting Environmental properties in .xml file:

The following xml file is used by an application deployed within Jboss.

Example: mail-service.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE server>

<!– $Id: javamail.html,v 1.3 2007/12/17 11:06:27 toufiq Exp $ –>


<!– Mail Connection Factory–>

<mbean code=”org.jboss.mail.MailService” name=”jboss:service=Mail”>

<attribute name=”JNDIName”>java:/Mail</attribute>

<attribute name=”User”></attribute>

<attribute name=”Password”>password</attribute>

<attribute name=”Configuration”>

 <!– Test –>


   <!– Change to your mail server prototocol –>

   <property name=”” value=”pop3″/>

   <property name=”mail.transport.protocol” value=”smtp”/>

   <!– Change to the user who will receive mail –>

   <property name=”mail.user” value=”nobody”/>

    <!– Change to the mail server –>

   <property name=”” value=””/>

   <!– Change to the SMTP gateway server –>

   <property name=”” value=””/>

    <!– Change to the address mail will be from –>

    <property name=”mail.from” value=””/>

   <!– Enable debugging output from the javamail classes –>

   <property name=”mail.debug” value=”false”/>






3.  Steps For Sending Email

1.Create a ‘Context’ and ‘Session’ objects to read mail properties using JNDI.

     Context namingContext = null;

      Session mailSession = null;

      namingContext = new InitialContext();

      mailSession = (Session)namingContext.lookup(“java:/Mail”);

2. Create a MimeMessage using Session Object

      MimeMessage mMessage = new MimeMessage(mailSession);

3. Create Store object to connect with mail server using username and password

      Store mailStore = null;

      mailStore = mailSession.getStore();


4. Set From address in MimeMessage object


5. Set To address in MimeMessage object

      mMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress, false));

6. Set Subject in MimeMessage object

mMessage.setSubject(“subject – hello world”); // Sets the Subject

7. Create and fill the first message part

MimeBodyPart mBodyPart = new MimeBodyPart();

mBodyPart.setText(“hi, test message”); // Sets the Body Message

8. Create the Multipart and its parts to it

Multipart l_mp = new MimeMultipart();


9. Add the Multipart to the message


10. Send Email using send method of Transport object


11. Close Store object