CommonsMailSender.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.mail;

  21. import java.io.File;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.UUID;

  25. import org.apache.commons.mail.DefaultAuthenticator;
  26. import org.apache.commons.mail.Email;
  27. import org.apache.commons.mail.EmailAttachment;
  28. import org.apache.commons.mail.MultiPartEmail;
  29. import org.apache.commons.mail.SimpleEmail;
  30. import org.openspcoop2.utils.UtilsException;
  31. import org.openspcoop2.utils.resources.FileSystemUtilities;
  32. import org.openspcoop2.utils.transport.http.SSLUtilities;
  33. import org.slf4j.Logger;

  34. /**
  35.  * CommonsMailSender
  36.  *
  37.  * @author Poli Andrea (apoli@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */
  41. public class CommonsMailSender extends Sender {

  42.     protected CommonsMailSender(Logger log) {
  43.         super(log);
  44.     }

  45.     @Override
  46.     public void send(Mail mail, boolean debug) throws UtilsException {
  47.        
  48.         List<File> filesAllegati = new ArrayList<File>();
  49.         try{
  50.        
  51.             boolean attach = (mail.getBody().getAttachments()!=null && mail.getBody().getAttachments().size()>0);
  52.             Email email = null;
  53.            
  54.             if(attach){
  55.                 email = new MultiPartEmail();
  56.             }else{
  57.                 email = new SimpleEmail();
  58.             }
  59.            
  60.             email.setSocketConnectionTimeout(this.getConnectionTimeout());
  61.             email.setSocketTimeout(this.getReadTimeout());
  62.            
  63.             email.setHostName(mail.getServerHost());
  64.             email.setSmtpPort(mail.getServerPort());
  65.             if(mail.getUsername()!=null){
  66.                 email.setAuthenticator(new DefaultAuthenticator(mail.getUsername(), mail.getPassword()));
  67.             }
  68.             if(mail.getSslConfig()!=null){
  69.                 email.setSSLCheckServerIdentity(mail.getSslConfig().isHostnameVerifier());
  70.                 email.setSSLOnConnect(true);
  71.                 StringBuilder bf = new StringBuilder();
  72.                 SSLUtilities.setSSLContextIntoJavaProperties(mail.getSslConfig(), bf);
  73.                 if(debug)
  74.                     this.log.debug(bf.toString());
  75.                 email.setStartTLSEnabled(mail.isStartTls());
  76.             }
  77.            
  78.             email.setFrom(mail.getFrom());
  79.             email.addTo(mail.getTo());
  80.             List<String> ccList = mail.getCc();
  81.             if(ccList!=null && ccList.size()>0){
  82.                 for (String cc : ccList) {
  83.                     email.addCc(cc);
  84.                 }
  85.             }
  86.            
  87.             email.setSubject(mail.getSubject());
  88.             if(attach){
  89.                
  90.                 if(mail.getBody().getMessage()!=null)
  91.                     email.setMsg(mail.getBody().getMessage());
  92.                
  93.                 for (MailAttach mailAttach : mail.getBody().getAttachments()) {
  94.                     EmailAttachment attachment = new EmailAttachment();
  95.                     File fTmp = null;
  96.                     if(mailAttach instanceof MailTextAttach){
  97.                          MailTextAttach text = (MailTextAttach) mailAttach;
  98.                          fTmp = FileSystemUtilities.createTempFile("mailTextAttach", ".txt");
  99.                          FileSystemUtilities.writeFile(fTmp, text.getContent().getBytes());
  100.                      }
  101.                      else{
  102.                          MailBinaryAttach bin = (MailBinaryAttach) mailAttach;
  103.                          fTmp = FileSystemUtilities.createTempFile("mailTextAttach", ".bin");
  104.                          FileSystemUtilities.writeFile(fTmp, bin.getContent());
  105.                      }
  106.                      attachment.setPath(fTmp.getAbsolutePath());
  107.                      filesAllegati.add(fTmp);
  108.                      attachment.setDisposition(EmailAttachment.ATTACHMENT);
  109.                      attachment.setDescription(mailAttach.getName());
  110.                      attachment.setName(mailAttach.getName());
  111.                      ((MultiPartEmail)email).attach(attachment);
  112.                 }
  113.             }else{
  114.                 email.setMsg(mail.getBody().getMessage());
  115.             }
  116.            
  117.             email.setDebug(debug);
  118.                        
  119.             if(mail.getUserAgent()!=null) {
  120.                 email.addHeader("User-Agent", mail.getUserAgent());
  121.             }
  122.            
  123.             if(mail.getMessageIdDomain()!=null) {
  124.                 email.addHeader("Message-ID", "<"+UUID.randomUUID().toString()+"@"+mail.getMessageIdDomain()+">");
  125.             }
  126.            
  127.             if(mail.getContentLanguage()!=null) {
  128.                 email.addHeader("Content-Language", mail.getContentLanguage());
  129.             }
  130.            
  131.             if(attach){
  132.                 email.buildMimeMessage();
  133.                 email.sendMimeMessage();
  134.             }else{
  135.                 email.send();
  136.             }
  137.         }catch(Exception e){
  138.             throw new UtilsException(e.getMessage(),e);
  139.         }
  140.         finally{
  141.             for (File file : filesAllegati) {
  142.                 try{
  143.                     if(!file.delete()) {
  144.                         // ignore
  145.                     }
  146.                 }catch(Throwable e){
  147.                     // ignore
  148.                 }
  149.             }
  150.         }
  151.        
  152.     }

  153. }