SendEmail.properties
__________________________________________
emailSender=ATS_UAT@xxx.com
emailHost=mlilhkmail01
emailProtocol=smtp
emailAuth=false
emailPort=25
emailDebug=false
errorEmailTo=josh_tl_kei@xxx.com;jimmy_kc_wong@xxx.com
errorEmailSubject=ATS Error

______________________________________________
package com.xxx.mil.application.yyy.util;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class SendEmail {

    private static boolean emailDebug = false;
    private static String emailFrom = "ATS@xxx.com";
    private static String emailHost = "mlilhkmail01";
    private static String emailPort = "25";
    private static String emailProtocol = "smtp";
    private static String emailAuth = "false";

    private static String emailTo = "";
    private static String emailCc = "";
    private static String emailBcc = "";
    private static String emailText = "";
    private static String emailSubject = "";

    private static String errorEmailTo = "";
    private static String errorEmailText = "";
    private static String errorEmailSubject = "";

    //private static String EMAIL_ATTACHMENT = "";
    private static ArrayList emailAttachmentList = new ArrayList();

    private static ResourceBundle bundle = null;

    public SendEmail() {
        try {
            bundle = ResourceBundle.getBundle("com.xxx.mil.application.yyy.SendEmail");
            String str = "";

            str = bundle.getString("emailSender");
            if(str != null && !"".equals(str)) {
                emailFrom = str;
            }

            str = bundle.getString("emailHost");
            if(str != null && !"".equals(str)) {
                emailHost = str;
            }

            str = bundle.getString("emailProtocol");
            if(str != null && !"".equals(str)) {
                emailProtocol = str;
            }

            str = bundle.getString("emailAuth");
            if(str != null && !"".equals(str)) {
                emailAuth = str;
            }

            str = bundle.getString("emailPort");
            if(str != null && !"".equals(str)) {
                emailPort = str;
            }

            str = bundle.getString("emailDebug");
            if(str != null && !"".equals(str)) {
                emailDebug = Boolean.getBoolean(str);
            }

            str = bundle.getString("errorEmailTo");
            if(str != null && !"".equals(str)) {
                errorEmailTo = str;
            }

            str = bundle.getString("errorEmailSubject");
            if(str != null && !"".equals(str)) {
                errorEmailSubject = str;
            }

         } catch(Exception e) {
            System.out.println("SendEmail.init Error: " + e.toString());
            e.printStackTrace();
        }

    }

    public boolean sendMail(String subject, ArrayList fileList, String text
        , String mailto, String mailcc, String mailbcc) throws Exception
    {
        boolean bMailSentFlag = false;
        MimeBodyPart mimebodypart = null;
        MimeMultipart mimemultipart = null;
        MimeMessage mimemessage = null;
        Session session = null;
        Transport transport = null;
        Properties props = null;
        String filename = "";

        Logger.writeLogln("Start to send email");

        if (hasReceiver(mailto, mailcc, mailbcc)) {
        props = new Properties();
        props.put("mail.transport.protocol",emailProtocol);
        props.put("mail.smtp.host", emailHost);
        props.put("mail.smtp.port", emailPort);
        props.put("mail.smtp.auth", emailAuth);

        session = Session.getDefaultInstance(props, null);
        session.setDebug(emailDebug);

        System.out.println("Mailing Process Started ............");
        try{
            //header : from , to , cc, subject
            mimemessage = new MimeMessage(session);
            mimemessage.setFrom(new InternetAddress(emailFrom));
            mimemessage.setSentDate(new java.util.Date());
            mimemessage.setSubject(subject);
            //mailto must be in the form of comma separated instead of semi-colon separated
            if(!mailto.equals(""))
            {
                InternetAddress[] ias = InternetAddress.parse(mailto);
                mimemessage.setRecipients(Message.RecipientType.TO, ias);
                System.out.println("EmailTO: " + mailto);
                Logger.writeLogln("EmailTO: " + mailto);
            }

            if(!mailcc.equals(""))
            {
                InternetAddress[] ias = InternetAddress.parse(mailcc);
                mimemessage.setRecipients(Message.RecipientType.CC,ias);
                System.out.println("EmailCC: " + mailcc);
                Logger.writeLogln("EmailCC: " + mailcc);
            }

            if(!mailbcc.equals(""))
            {
                InternetAddress[] ias = InternetAddress.parse(mailbcc);
                mimemessage.setRecipients(Message.RecipientType.BCC,ias);
                System.out.println("EmailBCC: " + mailbcc);
                Logger.writeLogln("EmailBCC: " + mailbcc);
            }

            //body
            mimemultipart = new MimeMultipart();
            //1. message
            mimebodypart = new MimeBodyPart();
            mimebodypart.setText(text);
            mimemultipart.addBodyPart(mimebodypart);

            Logger.writeLogln("Content: " + text);

            //2. file attachment
            try{
                if (fileList != null) {
                    for (int i=0; i<fileList.size(); i++) {
                      filename = (String)fileList.get(i);
                      System.out.println("Attaching file..." + filename);
                      mimebodypart = new MimeBodyPart();
                      FileDataSource filedatasource = new FileDataSource(filename);
                      mimebodypart.setDataHandler(new DataHandler(filedatasource));
                      mimebodypart.setFileName(filename); // set FILENAME
                      mimemultipart.addBodyPart(mimebodypart);
                    }
                }
            } catch(Exception exception3) {
                bMailSentFlag = false;
                System.out.println("Error in sending file not been able to attach ......\n" + exception3.getMessage());
            }

            mimemessage.setContent(mimemultipart);

            System.out.println("Sending mail in progress...");
            Transport.send(mimemessage);
            System.out.println("Sent Successfully..........");
        } catch(Exception e){
            bMailSentFlag = false;
            System.out.println("SendEmail.send Error: " + e.toString());
            e.printStackTrace();
            Logger.writeLogln(e.getMessage());
        }
        bMailSentFlag = true;
        System.out.println("Mailing Process Ended ............");
        } else
          System.out.println("No email receiver.");

        Logger.writeLogln("Send email completed");

        return bMailSentFlag;
    }

    public boolean sendErrorEmail(Exception e) throws Exception {

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String subject = errorEmailSubject + " from " + Utility.getHostName() + " @ " + dateFormat.format(now);

        return sendMail(subject, null, sw.toString(), errorEmailTo, "", "");
    }

    public boolean sendErrorMail(String text) throws Exception {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String subject = errorEmailSubject + " from " + Utility.getHostName() + " @ " + dateFormat.format(now);
        
        return sendMail(subject, null, text, errorEmailTo, "", "");
    }

    private boolean hasReceiver(String s1, String s2, String s3)
    {
      String str = "";
      if (s1 != null)
        str += s1;
      if (s2 != null)
        str += s2;
      if (s3 != null)
        str += s3;

      return (str.length() >0 ? true:false);
    }

    public static void main(String[] args) {
    }

    private void prepare(String reportType, String feedFileName) throws Exception
    {
        String propertiesPrefix = "";
        String attachment = "";
        try {

          if ("REPORT".equalsIgnoreCase(reportType)) {
            propertiesPrefix = "Report.";
          }
          this.emailAttachmentList.clear();

          emailSubject = bundle.getString(propertiesPrefix + "emailSubject");
          emailTo = bundle.getString(propertiesPrefix + "emailTo");
          emailCc = bundle.getString(propertiesPrefix + "emailCc");
          emailBcc = bundle.getString(propertiesPrefix + "emailBcc");
          emailText = bundle.getString(propertiesPrefix + "emailText");
          if (!"".equals(feedFileName)) {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(feedFileName));
            attachment = (String)in.readObject();
            emailAttachmentList.add(attachment);
          }

        } catch (Exception e)
        {
          e.printStackTrace();
          throw e;
        }
    }

}