package mail; 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; import shst.companydataloaders.SIPRPPropertiesLoader; import com.evolute.utils.error.ErrorLogger; public class Mail { public final static String ENDERECO_ENVIO = "planos.auchan@siprp.pt"; private final static String SMTP_HOST = "red.evolute.pt"; private final static int SMTP_PORT = 587; // private final String SMTP_USERNAME = "servico@siprp.pt"; private final String SMTP_USERNAME = ENDERECO_ENVIO; private final String SMTP_PASSWORD = "GR33%df5"; private final String BCC_1 = "departamentotecnico@siprp.pt"; private final String DEBUG_EMAIL_TO = "fpalma@evolute.pt"; public void send( String emailTo, String emailFrom, MailNextPhase nextPhase ) throws Exception { send( emailTo, emailFrom, nextPhase.getSubject(), nextPhase.getBody() ); } public void send( String emailTo, String emailFrom, String assunto, String texto_email ) throws Exception { Boolean isDebug = SIPRPPropertiesLoader.getInstance().findProperty( "debug", false ); Properties props = new Properties(); props.put("mail.smtp.host", SMTP_HOST ); props.put("mail.from", emailFrom ); props.put("mail.smtp.auth", "true" ); props.put("mail.smtp.user", SMTP_USERNAME ); props.put("mail.smtp.password", SMTP_PASSWORD ); Session session = Session.getDefaultInstance( props ); Message msg = new MimeMessage( session ); msg.setFrom( new InternetAddress( emailFrom ) ); InternetAddress[] address = new InternetAddress[] { new InternetAddress( isDebug ? DEBUG_EMAIL_TO : emailTo ) }; msg.setRecipients( Message.RecipientType.TO, address ); // for debuging purposes, we dont want to send emails to anyone else ! if ( ! isDebug ) { InternetAddress[] addressBCC = new InternetAddress[] { new InternetAddress( BCC_1 ) }; msg.setRecipients( Message.RecipientType.BCC, addressBCC ); } ( ( MimeMessage ) msg ).setSubject( assunto, "ISO-8859-1" ); msg.setSentDate( new Date() ); Multipart multipart = new MimeMultipart(); BodyPart msgBodyPart = new MimeBodyPart(); String html; html=" "; html+=""; html+= texto_email; html+=""; 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(); 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; } } }