package com.mdbitz.utils;

/*
 * Import Statements
 */
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;


/**
 * Mail Utility to send Emails with Basic User Password
 * Authentication
 */
public class BasicAuthMailUtil {

	/**
     * SMTP Mail Host
     */
    private static String HOST = "YOUR SMTP MAIL HOST";
    
    /**
     * User name
     */
    private static String AUTH_USER = "USER NAME";
    
    /**
     * Password
     */
    private static String AUTH_PSWD = "PASSWORD";
    
    /**
     * Send an email
     * @param recipients recipients list
     * @param subject subject of mail
     * @param message message to be sent
     * @param from from address
     * @throws MessagingException
     */
    public static void postMail( String recipients[], String subject,
            String message, String from  ) throws MessagingException
    {
        BasicAuthMailUtil.postMail( recipients, subject, message, from, null );
    }
    
    /**
     * Send an email
     * @param recipients recipients list
     * @param subject subject of mail
     * @param message message to be sent
     * @param from from address
     * @param ccRecipients CC Recipients list
     * @throws MessagingException
     */
    public static void postMail( String recipients[], String subject,
            String message, String from, String ccRecipients[] )
            throws MessagingException
    {
        // Create properties
        Properties props = new Properties();
        props.put("mail.smtp.host", HOST);
        props.put("mail.debug", "true");
        
        // Get Session
        Authenticator auth = new BasicSMTPAuthenticator();
        Session sess = Session.getDefaultInstance( props, auth );
        
        // Instantiatee a message
        Message msg = new MimeMessage(sess);

		// from
        msg.setFrom(new InternetAddress(from));
        
        // recipients
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for( int i=0; i < recipients.length; i ++ ) {
            addressTo[i] = new InternetAddress( recipients[i] );
        }
        msg.setRecipients( Message.RecipientType.TO, addressTo );
        
        // ccRecipients
        if( ccRecipients != null && ccRecipients.length > 0 ) {
            InternetAddress[] addressCC = new InternetAddress[ccRecipients.length];
            for( int i=0; i < ccRecipients.length; i ++ ) {
                addressCC[i] = new InternetAddress( ccRecipients[i] );
            }
            msg.setRecipients( Message.RecipientType.CC, addressCC );
        }
        
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        msg.setContent( message, "text/html" );
        Transport.send( msg );
    }
    
    /**
     * Authenticator for Basic SMTP UserName & Password Authentication
     */
    private class BasicSMTPAuthenticator extends javax.mail.Authenticator
    {
        public PasswordAuthentication getPasswordAuthentication()
        {
          return new PasswordAuthentication( AUTH_USER, AUTH_PSWD );
        }
    }
    
}
