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.

mail.store.protocol:

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

mail.transport.protocol:

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

mail.host:

It specifies the default Mail server.

mail.user:

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

mail.protocol.host:

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

mail.protocol.user:

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

mail.from:

It specifies the return address of the current user.

mail.debug:

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 $ –>

<server>>

<!– Mail Connection Factory–>

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

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

<attribute name=”User”>abc@adhisoftware.com</attribute>

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

<attribute name=”Configuration”>

 <!– Test –>

  <configuration>

   <!– Change to your mail server prototocol –>

   <property name=”mail.store.protocol” 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=”mail.pop3.host” value=”mail.adhisoftware.com”/>

   <!– Change to the SMTP gateway server –>

   <property name=”mail.smtp.host” value=”mail.adhisoftware.com”/>

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

    <property name=”mail.from” value=”abc@adhisoftware.com”/>

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

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

  <configuration>

 <depends>jboss:service=Naming</depends>

</attribute>

</mbean>

<server>

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();

      mailStore.connect();

4. Set From address in MimeMessage object

      mMessage.setFrom();

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();

     
l_mp.addBodyPart(mBodyPart);

9. Add the Multipart to the message

     
mMessage.setContent(l_mp);

10. Send Email using send method of Transport object

     
Transport.send(mMessage);

11. Close Store object

     mailStore.close();