|
|
|
|
@ -1,10 +1,6 @@
|
|
|
|
|
/*
|
|
|
|
|
* To change this template, choose Tools | Templates
|
|
|
|
|
* and open the template in the editor.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package mail;
|
|
|
|
|
|
|
|
|
|
import com.evolute.utils.error.ErrorLogger;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
import javax.mail.BodyPart;
|
|
|
|
|
@ -17,47 +13,44 @@ 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";
|
|
|
|
|
public class Mail
|
|
|
|
|
{
|
|
|
|
|
private final static String SMTP_HOST = "mail.evolute.pt";
|
|
|
|
|
private final static int SMTP_PORT = 587;
|
|
|
|
|
|
|
|
|
|
private final String mail_username = "acidentes.auchan@siprp.pt";
|
|
|
|
|
// private final String mail_password = "47Ju6Vb";
|
|
|
|
|
private final String mail_password = "EghRzS2l";
|
|
|
|
|
private final String SMTP_USERNAME = "acidentes.auchan@siprp.pt";
|
|
|
|
|
private final String SMTP_PASSWORD = "EghRzS2l";
|
|
|
|
|
|
|
|
|
|
public final static String ENDERECO_ENVIO = "planos.auchan@siprp.pt";
|
|
|
|
|
//public final static String ENDERECO_ENVIO = "lluis@evolute.pt"; //testes
|
|
|
|
|
|
|
|
|
|
private final String BCC_1 = "departamentotecnico@siprp.pt";
|
|
|
|
|
|
|
|
|
|
public void send(String emailTo, String emailFrom, String assunto, String texto_email) throws Exception
|
|
|
|
|
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");
|
|
|
|
|
props.put("mail.smtp.user", SMTP_USERNAME );
|
|
|
|
|
props.put("mail.smtp.password", SMTP_PASSWORD );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Session session = Session.getInstance( props );
|
|
|
|
|
// Authenticator auth = new SMTPAuthenticator();
|
|
|
|
|
// Session session = Session.getDefaultInstance(props, auth);
|
|
|
|
|
Session session = Session.getDefaultInstance( props );
|
|
|
|
|
|
|
|
|
|
Message msg = new MimeMessage( session );
|
|
|
|
|
msg.setFrom( new InternetAddress( emailFrom ) );
|
|
|
|
|
InternetAddress[] address = {new InternetAddress( emailTo )};
|
|
|
|
|
InternetAddress[] address = new InternetAddress[] {
|
|
|
|
|
new InternetAddress( emailTo )
|
|
|
|
|
};
|
|
|
|
|
msg.setRecipients( Message.RecipientType.TO, address );
|
|
|
|
|
((MimeMessage)msg).setSubject(assunto, "UTF-8");
|
|
|
|
|
msg.setSentDate(new Date());
|
|
|
|
|
|
|
|
|
|
InternetAddress[] addressBCC = new InternetAddress[] {
|
|
|
|
|
new InternetAddress( BCC_1 )
|
|
|
|
|
};
|
|
|
|
|
msg.setRecipients( Message.RecipientType.BCC, addressBCC );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
( ( MimeMessage ) msg ).setSubject( assunto, "UTF-8" );
|
|
|
|
|
msg.setSentDate( new Date() );
|
|
|
|
|
Multipart multipart = new MimeMultipart();
|
|
|
|
|
BodyPart msgBodyPart = new MimeBodyPart();
|
|
|
|
|
String html;
|
|
|
|
|
@ -65,29 +58,35 @@ public class Mail {
|
|
|
|
|
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 );
|
|
|
|
|
msgBodyPart.setContent( html, "text/html" );
|
|
|
|
|
multipart.addBodyPart( msgBodyPart );
|
|
|
|
|
msg.setContent( multipart );
|
|
|
|
|
|
|
|
|
|
String log = "Mail . send() :\n" +
|
|
|
|
|
"To : " + emailTo + ", From : " + emailFrom + "\n" +
|
|
|
|
|
"Subject : " + assunto + "\n" +
|
|
|
|
|
"Message Body :\n" + texto_email + "\n" +
|
|
|
|
|
"\n" +
|
|
|
|
|
"Result : ";
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Transport t = session.getTransport( "smtp" );
|
|
|
|
|
t.connect( SMTP_HOST, SMTP_PORT, SMTP_USERNAME, SMTP_PASSWORD );
|
|
|
|
|
t.sendMessage( msg, msg.getAllRecipients() );
|
|
|
|
|
|
|
|
|
|
t.close();
|
|
|
|
|
// System.out.println("Email Enviado !!!! ");
|
|
|
|
|
|
|
|
|
|
log += "sent !\n";
|
|
|
|
|
ErrorLogger.log( log );
|
|
|
|
|
}
|
|
|
|
|
catch ( Exception e )
|
|
|
|
|
{
|
|
|
|
|
log += "error occurred : " + e.getMessage() + "\n(See next error log (should be this exception stacktrace!)\n";
|
|
|
|
|
ErrorLogger.log( log );
|
|
|
|
|
// note: no need to call log( e ) as it should be caught be who called this method ..
|
|
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// private class SMTPAuthenticator extends Authenticator {
|
|
|
|
|
// public PasswordAuthentication getPasswordAuthentication() {
|
|
|
|
|
// String username = "lluis";
|
|
|
|
|
// String password = "654321";
|
|
|
|
|
// return new PasswordAuthentication(username, password);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|