/*
 * 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 SERVIDOR_SMTP = "mail2.evolute.pt";
private final static String SERVIDOR_SMTP_PASSWORD = "EVOLUTE";    
//public final static String ENDERECO_ENVIO = "acidentes.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", SERVIDOR_SMTP); 
        props.put("mail.from", emailFrom); 
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.user", "lluis");
        props.put("mail.smtp.password", "654321");        
    //        Session session1 = 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); 
        System.out.println("Email Enviado !!!! ");         

    }    

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

