/* $Id: MailUtil.java,v 1.1 2008/06/18 12:29:33 ahmed Exp $ */


package com.util;


import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Provider;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * MailUtil is used to send the mail.It uses the javax.mail to send the mail.
 *  * 
 * @version 1.0
 */


public class MailUtil  {
    
    HttpSession session;
    HttpServletRequest request ;
    public MailUtil (HttpServletRequest request) {
        this.request = request;
        session = request.getSession();
    }
    
    public MailUtil()
    {

    }
   
    static Properties userprops= URLDataHandler.getUserSettings();
    public static String user =userprops.getProperty( "mailUserName");//DBConfigParser.getInstance().getMailServerUserName();
    public static String passwd =userprops.getProperty( "mailPassword"); //DBConfigParser.getInstance().getMailServerPassword();
    public static String mailserver =userprops.getProperty( "mailServerName");// DBConfigParser.getInstance().getMailServerName();
    public static String mailport =userprops.getProperty( "mailServerPort");//DBConfigParser.getInstance().getMailServerPort();
    public static String mailtimeout =userprops.getProperty( "mailTimeout"); //DBConfigParser.getInstance().getMailTimeout();
    
    /**
     * This method will set the mail server details to a properties,which have to be set for the mail session.
     * 
     * @return Properties
     */
    public Properties getMailProps() {
        Properties props = new Properties();
       // EntrepreneurUtil.printInLog("Mail server IN getMailProps:::" + MailUtil.mailserver,"Debug");
        props.put("mail.smtp.host", MailUtil.mailserver);
        props.put("mail.smtp.port", new Integer(MailUtil.mailport));
        //since default timeout is infinite, we have to set the limit
        props.put("mail.smtp.connectiontimeout","10000");
        props.put("mail.smtp.timeout","10000");
        props.put("mail.smtp.auth", "true");
        return props;
    }
    
    /**
     * This is the main method which will send the mail using the javax.mail.transport
     * 
     * It will return wether mail send is success or failure.
     * 
     * @return String
     */
    
    public String despatch(String subject , String email , String content,String contentType,String sender)
    {
      //  EntrepreneurUtil.printInLog(" despatch Properties ::subject::" + subject + "::email::" + email + "::sender::" +  sender + "::contentType::" +  contentType + "::sender::" + sender,"Debug");
        String result = "";
        try 
        {
            //Properties props = getMailProps();
            Properties props = new Properties();
            props.put("mail.smtp.host",MailUtil.mailserver);
            props.put("mail.smtp.port",new Integer(MailUtil.mailport));
            props.put("mail.smtp.timeout", MailUtil.mailtimeout);
            
            Authenticator authen = null;
            Session session = Session.getDefaultInstance(props,authen);
            /*System.out.println("Session Properties -------->" + session.getProperties());
            System.out.println("Providers");
            Provider[] p = session.getProviders();
            for (int i = 0; i < p.length; i++)
            {
                System.out.println("Provider protocol: "+p[i].getProtocol());
            }
            System.out.println(session.getProviders().toString());*/
           // EntrepreneurUtil.printInLog("Mail Session Properties :::" + session.getProperties(),"Debug");
            Message msg = new MimeMessage(session);
            String senderName = sender;

            msg.setSubject(subject);
            InternetAddress fromAddr = new InternetAddress("noreply@adventnet.com" ,senderName);
            msg.setFrom(fromAddr);
            msg.setSentDate(new java.util.Date());
            InternetAddress toAdd = new InternetAddress(email);
            //setReplyToAdd(msg);
            msg.addRecipients(Message.RecipientType.TO , new InternetAddress[] { toAdd } );

            msg.setContent(content, contentType+"; charset=UTF-8");
            msg.addHeader("Content-Transfer-Encoding","base64");
            Transport transport = session.getTransport("smtp"); //No Internationalization
            //System.out.println("Before send :::");
            //transport.connect("smtp","sujai","sujai");
            transport.send(msg);
            result="sucess";
            return result;
        }
        catch (Exception exp) 
        {
            exp.printStackTrace();
           //EntrepreneurUtil.printInLog(" MailUtil:Error while Sending Mail."
             //    +"subject::"+subject+"::email::"+email+"::Error::"+exp.getMessage(),"normal");
            result="failure";
            return result;
        }
    }
} // MailUtil
