
/*
 * MailBuilder.java
 *
 * Created on 27 de julio de 2004, 2:21
 */

/**
 *
 * @author  Mario Linares Vásquez
 */

package com.fenalco.utilities.mail;

import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;



public class MailHandler
{
    
    private static MailHandler mailHandler;
    private String smtpHost;
    
    private MailHandler()
    {
        try
        {
            Properties props = new Properties();
//            InputStream entrada = Object.class.getClass().getResourceAsStream( "../config/config.properties" );
            InputStream entrada = getClass().getResourceAsStream( "../config/config.properties" );
            if( entrada == null )
            {
                  throw new Exception( "File nof found: config.properties" );
            }

            props.load( entrada );
            entrada.close();
            smtpHost = props.getProperty( "mail-server" );
        
        }
        catch(Exception e ){
            System.out.println(e.getMessage());
            
        }
        
    }
    
    public static synchronized MailHandler getInstance()
    {
        if( mailHandler == null )
        {
            mailHandler = new MailHandler();
        }
        return mailHandler;
    }
    
    
    public boolean sendMail(String from,String to,String subject,String message)
    {    
         boolean flag = true;
        try
        {    
            Properties props = new Properties();
            props.put("mail.smtp.host", smtpHost);

            System.out.println(smtpHost);

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

            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(RecipientType.TO, address);
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            // crea y llena la primera aprte del mensaje
            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(message);
            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
            msg.setContent(mp);
            // envía el mensaje
            Transport.send(msg);
        
        }
        catch(Exception e )
        {
            System.out.println("Excepcion en MailHanlder-->"+e.getMessage());
            flag = false;
        }
       return false;
    }
        
 
    
 /*   public static void  main(String args[])
     {
         
        MailHandler mh = MailHandler.getInstance();
         mh.sendMail("mlinarev@hotmail.com", "romeroandres@hotmail.com", "saludo de pepe", "hola mario este es un saludo de pepe sdsdsdsdsdsdsdsdsdsd");
        
            
    }*/
        
}
    
    
    

