Send email through Java

import java.util.ArrayList;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {

private static String exchangeServer = “400exch1”;
private static String subject;
private static String fromAddress;
private static String footer;

private static String getSubject() {
return subject;

private static void setSubject(String aSubject) {
subject = aSubject;

private static String getFromAddress() {
return fromAddress;

private static void setFromAddress(String aFromAddress) {
fromAddress = aFromAddress;

private static String getFooter() {
return footer;

private static void setFooter(String aFooter) {
footer = aFooter;

public SendMail(String subj, String fromAdd, String footerMsg) {
subject = subj;
fromAddress = fromAdd;
footer = footerMsg;

private static void send(ArrayList to, String from, String host, String message) {
if (to == null || from == null || message == null || host == null) {
Properties props = System.getProperties();
props.put(“”, host);
Session session = Session.getDefaultInstance(props, null);
if (session == null) {
MimeMessage mm = new MimeMessage(session);
try {
Address addresses[] = new Address[to.size()];
for (int i = 0; i 1) {
for (int j = 0; j < addresses.length – 1; j++) { mm.addRecipients(javax.mail.Message.RecipientType.CC, addresses[j + 1].toString()); } } mm.setSubject(getSubject()); mm.setContent((new StringBuilder()).append(message).append( "

\n\n\n** Do not reply to this message ***
} catch (AddressException ex) {
} catch (MessagingException ex) {

public void sendEmail(String message, ArrayList emailList) {
if (message == null) {
} else {
send(emailList, getFromAddress(), exchangeServer, message);


Constructor Detail


public SendMail(java.lang.String subj,

                java.lang.String fromAdd,

                java.lang.String footerMsg)


subj – Subject of the email     (String)

fromAdd – From Address   (String)

footerMsg – Set Custom footer message – optional (String)


Method Detail


public void sendAlert(java.lang.String message,

                      java.util.ArrayList emailList)


message – Message body to be sent in the email- Text or HTML

The Html Body could contain image with a valid link

emailList – Array list of the email addresses the email to be sent.

For example to send to email –

To send to Email Groups just add the email group to the array list

For example to send email to CIM Group add “#CIM” to the array list.


Sample Code

SendMail sm = new SendMail(“Test Email”, “Anonymous”, “Please do not reply to this email”);

ArrayList addressList = new ArrayList();


sm.sendAlert(“This is a Test Message”, addressList);


One thought on “Send email through Java

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s