Let's talk Contact us. No cost. No obligation.

Fill out this form and we will contact you with in 24 hrs.

Knowledge Base

Sending Email using JavaMail API in Adempiere System

April 22, 2014

Introduction to java mail API
The JavaMail is an API that is used to compose, write and read electronic messages.
The JavaMail API provides protocol-independent and plateform-independent framework for sending and receiving mails

Uses of JAVA Mail in an application:
The JavaMail facility can be applied to many events.

=>It can be used at the time of registering the user (sending notification such as thanks for your interest to my site),
=>forgot password (sending password to the users email id), sending notifications for important updates etc. So there can be various usage of java mail api

Protocols used in JavaMail API:

There are some protocols used in JavaMail api such as SMTP(Simple Mail Transfer Protocol), POP(Post Office Protocol), MIME(Multiple Internet Mail Extension) ,IMAP etc.

javaMail

Steps to send Mail:
NOTE: The javax.mail and javax.mail.activation packages contains the core classes of JavaMail API.

1) Get Session Object:

The javax.mail.Session class provides two methods to get the object of session, Session.getDefaultInstance() method and Session.getInstance() method. You can use anyone method to get the session object.

Properties properties=new Properties();
//fill all the informations like host name etc.
Session session=Session.getInstance(properties,null);

2) Compose the Messsage:

The javax.mail.Message class provides methods to compose the message. But it is an abstract class so its subclass

javax.mail.internet.MimeMessage class is mostly used.
MimeMessage message=new MimeMessage(session);
MimeMessage class contains various methods such as:

=> public void setFrom(Address address): is used to set the from header field.
=> public void addRecipients(Message.RecipientType type, String addresses): is used to add the given address to the recipient type.
=> public void setSubject(String subject): is used to set the subject header field.
=> public void setText(String textmessage): is used to set the text as the message content using text/plain MIME type.

3) Send The Message: The javax.mail.Transport class provides method to send the message.

=>public static void send(Message message): is used send the message.
=>public static void send(Message message, Address[] address): is used send the message to the given addresses.

Note: For sending the email using JavaMail API, we need to load the two jar files:

a) mail.jar
b) activation.jar

Code Snippet:

import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*;
public class SendEmail {
public static void main(String [] args) {
String to = “onsinteractive@gmail.com”;
String from = “profitbysearch@gmail.com”;
String host = “localhost”;//or IP address
//Get the session object
Properties properties = System.getProperties();
properties.setProperty(“mail.smtp.host”, host);
Session session = Session.getDefaultInstance(properties);
//compose the message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(“Ping”);
message.setText(“Hello, this is example of sending email”);
// Send message
Transport.send(message);
System.out.println(“message sent successfully….”);
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}