/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mail;

import javax.mail.*;  
import javax.mail.Authenticator;
import java.util.Date;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author lluis
 */
public class Mail {


	private final static String SMTP_HOST = "mail2.evolute.pt";
	private final static int SMTP_PORT = 587;

	private final String mail_username = "acidentes.auchan@siprp.pt";
	private final String mail_password = "47Ju6Vb";

	public final static String ENDERECO_ENVIO = "planos.auchan@siprp.pt";
	//public final static String ENDERECO_ENVIO = "lluis@evolute.pt";    //testes


    public void send(String emailTo, String emailFrom, String assunto, String texto_email) throws Exception  
    {
        Properties props = new Properties(); 
        //props.put("mail.transport.protocol", "smtp"); 
        props.put("mail.smtp.host", SMTP_HOST );
        props.put("mail.from", emailFrom );
        props.put("mail.smtp.auth", "true" );
		props.put("mail.smtp.user", mail_username );
        props.put("mail.smtp.password", mail_password );
//        props.put("mail.smtp.user", "lluis");
//        props.put("mail.smtp.password", "654321");


		Session session = Session.getInstance( props );
//        Authenticator auth = new SMTPAuthenticator();
//        Session session = Session.getDefaultInstance(props, auth);

        Message msg = new MimeMessage( session );
        msg.setFrom( new InternetAddress( emailFrom ) );
        InternetAddress[] address = {new InternetAddress( emailTo )};
        msg.setRecipients( Message.RecipientType.TO, address );
        ((MimeMessage)msg).setSubject(assunto, "UTF-8");
        msg.setSentDate(new Date()); 
        Multipart multipart = new MimeMultipart();
        BodyPart msgBodyPart = new MimeBodyPart();
        String html;    
        html="<html><head>  <meta content='text/html;charset=ISO-8859-1' http-equiv='Content-Type'></head>";
        html+="<body bgcolor='#ffffff' text='#000000'>";
        html+= texto_email;
        html+="<body></html>";
        msgBodyPart.setContent(html, "text/html");
        multipart.addBodyPart(msgBodyPart);
        msg.setContent(multipart);
		
//        Transport.send(msg);
		Transport t;
		t = session.getTransport( "smtp" );

		t.connect( SMTP_HOST, SMTP_PORT, mail_username, mail_password );
		t.sendMessage( msg, msg.getAllRecipients() );

		t.close();
        System.out.println("Email Enviado !!!! ");         

    }    

//     private class SMTPAuthenticator extends Authenticator {
//         public PasswordAuthentication getPasswordAuthentication() {
//         String username = "lluis";
//         String password = "654321";
//         return new PasswordAuthentication(username, password);
//     }
//
//     }
}

