Java tutorial
/* * Copyright (C) 2016 Dominion Global * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.dominion.salud.pedicom.negocio.tools; import com.dominion.salud.pedicom.negocio.configuration.RoutingDataSource; import com.dominion.salud.pedicom.negocio.entities.Datasources; import com.dominion.salud.pedicom.negocio.entities.Datos; import java.io.InputStream; import javax.activation.DataSource; import javax.mail.util.ByteArrayDataSource; import org.apache.commons.lang3.StringUtils; import org.apache.commons.mail.DefaultAuthenticator; import org.apache.commons.mail.MultiPartEmail; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author j.contreras-ext */ @Service("mailService") public class MAILService { private static final Logger logger = LoggerFactory.getLogger(MAILService.class); @Autowired private Environment environment; @Autowired private Datos allDataSources; @Autowired private RoutingDataSource routingDataSource; /** * configura el ciente de correo y envia el correo con un adjunto * * @param to * @param attach el archivo que se adjunta en el correo * @param centro nombre del centro desde el que se envia * @throws Exception */ public void sendByMail(String to, Object attach, String centro) throws Exception { logger.debug(" Iniciando la configuracion del mail con sus parametros correspondientes ," + " obtenemos los parametros de environment"); String dataBase = routingDataSource.dbActual(); allDataSources.getDatasources(); Datasources dat = null; for (Datasources datas : allDataSources.getDatasources()) { if (datas.getNombreDatasource().equals(dataBase)) { dat = datas; break; } } if (StringUtils.isBlank(StringUtils.trim(dat.getHost()))) { throw new Exception( "El host es [" + StringUtils.trim(dat.getHost()) + "] , error de host desconocido o erroneo"); } logger.debug(" El host es [" + StringUtils.trim(dat.getHost()) + "]"); if (StringUtils.isBlank(StringUtils.trim(dat.getUsernameEmail()))) { throw new Exception("El usuario es [" + StringUtils.trim(dat.getUsernameEmail()) + "], error de login en el correo , usuario desconocido o erroneo"); } logger.debug(" El usuario es [" + StringUtils.trim(dat.getUsernameEmail()) + "]"); if (StringUtils.isBlank(StringUtils.trim(dat.getPasswordEmail()))) { throw new Exception("La contrasea es [" + StringUtils.trim(dat.getPasswordEmail()) + "], error de login en el correo , contrasea desconocida o erronea"); } logger.debug(" La contrasea es [" + StringUtils.trim(dat.getPasswordEmail()) + "]"); if (StringUtils.isBlank(to)) { throw new Exception("El destinatario es [" + to + "], no se especificaron destinatarios."); } logger.debug(" El destinatario es [" + to + "]"); /*if (dat.getPort()== null) { throw new Exception("El puerto es [" + StringUtils.trim(environment.getProperty("mail.port")) + "], el puerto del servidor de correo falla"); }*/ logger.debug(" El puerto es [" + dat.getPort() + "]"); /*if (StringUtils.isBlank(StringUtils.trim(environment.getProperty("mail.TLS")))) { throw new Exception("El TLS es [" + StringUtils.trim(environment.getProperty("mail.TLS")) + "]"); }*/ logger.debug(" El TLS es [" + dat.getTLS() + "]"); System.setProperty("mail.imap.auth.plain.disable", "true"); MultiPartEmail email = new MultiPartEmail(); email.setHostName(StringUtils.trim(dat.getHost())); email.setAuthenticator(new DefaultAuthenticator(StringUtils.trim(dat.getUsernameEmail()), StringUtils.trim(dat.getPasswordEmail()))); email.setFrom(StringUtils.trim(StringUtils.trim(dat.getUsernameEmail())), centro); email.setDebug(true); email.setSmtpPort(dat.getPort()); email.setStartTLSEnabled(dat.getTLS()); email.setSSLOnConnect(dat.getSSL()); DataSource source = null; if (attach != null) { logger.debug(" Adjuntando pdf"); source = new ByteArrayDataSource((InputStream) attach, "application/pdf"); email.attach(source, "Pedido de centro " + centro + ".pdf", "Pedido de centro " + centro); } email.setSubject("Pedido de centro [" + centro + "]"); logger.debug(" Realizando envio"); email.addTo(to); email.send(); try { if (!StringUtils.isBlank(StringUtils.trim(dat.getMailCC()))) { logger.debug(" Enviando a CC: " + dat.getMailCC()); MultiPartEmail emailCC = new MultiPartEmail(); emailCC.setHostName(StringUtils.trim(dat.getHost())); emailCC.setAuthenticator(new DefaultAuthenticator(StringUtils.trim(dat.getUsernameEmail()), StringUtils.trim(dat.getPasswordEmail()))); emailCC.setFrom(StringUtils.trim(StringUtils.trim(dat.getUsernameEmail())), centro); emailCC.setDebug(true); emailCC.setSmtpPort(dat.getPort()); emailCC.setStartTLSEnabled(dat.getTLS()); emailCC.setSSLOnConnect(dat.getSSL()); if (attach != null) { logger.debug(" Adjuntando pdf a copia"); emailCC.attach(source, "Pedido de centro " + centro + ".pdf", "Pedido de centro " + centro); } emailCC.setSubject("Pedido de centro " + centro); emailCC.addCc(StringUtils.split(StringUtils.trim(dat.getMailCC()), ",")); emailCC.send(); } } catch (Exception e) { logger.warn(" Se han producido errores al enviar los CC: " + e.toString()); } logger.debug(" Finalizando envio email"); } }