package siprp.medicina.processo.mail;

import java.util.Date;
import java.util.Properties;

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

public class MailSender
{
	protected String from = "fpalma@evolute.pt";
	protected String mailServer = "mail2.evolute.pt";
	protected String userName = "fpalma@evolute.pt";
	protected String password = "";
	
	public MailSender()
	{
		
	}

	public void send( String to, String bcc, String subject, String body,
			String attachmentNames[], byte attachments[][] )
		throws Exception
	{
		Properties props = System.getProperties();
		props.put( "mail.smtp.host", mailServer );
	    props.put( "mail.smtp.auth", "true" );

	    Authenticator auth = new SMTPAuthenticator();
		Session session = Session.getInstance( props, auth );
		
		Message msg = new MimeMessage( session );
		
		msg.setFrom( new InternetAddress( from ) );
		msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to, true ) );
		msg.setRecipients( Message.RecipientType.BCC, InternetAddress.parse( bcc, true ) );
		msg.setSubject( subject );
		msg.setContent( body, "text/html" );

		msg.setHeader( "X-Mailer", "Evolute Mailer" );
		msg.setSentDate( new Date() );
		Transport.send( msg );
	}
	
	private class SMTPAuthenticator extends javax.mail.Authenticator
	{

	    public PasswordAuthentication getPasswordAuthentication()
	    {
	        return new PasswordAuthentication(userName, password);
	    }
	}
}
