Monday, June 10, 2013

Send mail using java (Exchange Server)


External jar needs to be added is "mail.jar".
========================================================
package com.myExample.mail;

public class Client {
      public static void main(String[] args) {
            String To = "abc1@xyz.com; abc2@xyz.com ; abc3@xyz.com ";
            String CC = " abc4@xyz.com; abc5@xyz.com ; abc6@xyz.com ";
            String BCC = "abc@xyz.com";
            String From = "myExmp@ddd.com";
            String Subject = "Test subject line.";
            String Body = "Test mail Body.";
            MailExample obj = new MailExample();
            obj.sendAMail(To, CC, BCC, From, Body, Subject);
      }
}

=========================================================
package com.myExample.mail;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailExample {
                public void sendAMail(String To, String CC, String BCC, String From,
                                                String Body, String Subject) {
                                try {
                                                Sendmail(To, CC, BCC, From, Body, Subject);
                                } catch (MessagingException ex) {
                                                Logger.getLogger(MailExample.class.getName()).log(Level.SEVERE,
                                                                                null, ex);
                                }
                }

                public void Sendmail(String To, String CC, String BCC, String From,
                                                String Body, String Subject) throws MessagingException {
                                Message message = new MimeMessage(getSession());
                                if (To != null && To.trim().length() > 0) {
                                                //Separate "To" mail address based on ";" separator and set it into mail "To" address.
                                                Iterator<String> ToIterator=this.getAddressList(To).iterator();
                                                while(ToIterator.hasNext()){
                                                                message.addRecipient(RecipientType.TO, new InternetAddress(ToIterator.next()));
                                                }
                                               
                                                if (CC != null && CC.trim().length() > 0){
                                                                //Separate "CC" mail address based on ";" separator and set it into mail "CC" address.
                                                                Iterator<String> CCIterator=this.getAddressList(CC).iterator();
                                                                while(CCIterator.hasNext()){
                                                                                message.addRecipient(RecipientType.CC, new InternetAddress(CCIterator.next()));
                                                                }
                                                }
                                                               
                                                if (BCC != null && BCC.trim().length() > 0){
                                                                //Separate "BCC" mail address based on ";" separator and set it into mail "BCC" address.
                                                                Iterator<String> BCCIterator=this.getAddressList(BCC).iterator();
                                                                while(BCCIterator.hasNext()){
                                                                                message.addRecipient(RecipientType.BCC, new InternetAddress(BCCIterator.next()));
                                                                }
                                                }
                                               
                                                if (From != null && From.trim().length() > 0)
                                                                message.addFrom(new InternetAddress[] { new InternetAddress(
                                                                                                From) });

                                                message.setSubject(Subject);
                                                message.setContent(Body, "text/plain");
                                }
                                System.out.println("Sending mail .............................");
                                Transport.send(message);
                                System.out.println("Mail sent .............................");
                }

                private Session getSession() {
                                Authenticator authenticator = new Authenticator();

                                Properties properties = new Properties();
                                properties.setProperty("mail.smtp.submitter", authenticator
                                                                .getPasswordAuthentication().getUserName());
                                properties.setProperty("mail.smtp.auth", "true");

                                properties.setProperty("mail.smtp.host", "172.16.16.99");
                                properties.setProperty("mail.smtp.port", "25");

                                return Session.getInstance(properties, authenticator);
                }

                private class Authenticator extends javax.mail.Authenticator {
                                private PasswordAuthentication authentication;

                                public Authenticator() {
                                                String username = "username";
                                                String password = password";
                                                authentication = new PasswordAuthentication(username, password);
                                }

                                protected PasswordAuthentication getPasswordAuthentication() {
                                                return authentication;
                                }
                }
               
                //Separate string using ';' separator.
                private List<String> getAddressList(String address){
                                String mailAddress=address;
                                List<String> mailAddressList = null;
                                if(mailAddress != null && mailAddress.trim().length()>0){
                                                mailAddressList = new ArrayList<String>();
                                                if(mailAddress.contains(";")){
                                                                mailAddressList=Arrays.asList(mailAddress.split(";"));
                                                }else{
                                                                mailAddressList.add(mailAddress.trim());
                                                }
                                }
                                return mailAddressList;
                }
}