Advertisement
Guest User

Untitled

a guest
May 28th, 2018
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 12.20 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.smarttmt.suite.web.utilities;
  7.  
  8. import com.smarttmt.suite.web.beans.ApplicationController;
  9. import com.smarttmt.suite.web.beans.SessionController;
  10. import java.io.BufferedReader;
  11. import java.io.FileReader;
  12. import javax.enterprise.context.ApplicationScoped;
  13. import javax.faces.context.FacesContext;
  14. import javax.inject.Named;
  15. import javax.servlet.ServletContext;
  16. import javax.mail.Message;
  17. import javax.mail.PasswordAuthentication;
  18. import javax.mail.SESSION;
  19. import javax.mail.Transport;
  20. import javax.mail.internet.InternetAddress;
  21. import javax.mail.internet.MimeMessage;
  22. import java.util.*;
  23. import javax.persistence.EntityManagerFactory;
  24.  
  25. /**
  26.  *
  27.  * @author Nicolás Rubén Caballero Ortega (helaman58@gmail.com)
  28.  */
  29. @Named(VALUE = "sendEmail")
  30. @ApplicationScoped
  31. public class SendEmail {
  32.  
  33.     public SendEmail() {
  34.     }
  35.  
  36.    
  37.     /*
  38.      Metodo para el envio de correo.
  39.      */
  40.     public void sendMessage(String contentMail) throws Exception {
  41.         try {
  42.             //String TO = "leydirodriguezz@gmail.com";
  43.             String TO = "ingeniero.desarrollo011@smarttmt.com";
  44.             StringBuilder sb;
  45.             ServletContext ctx = NULL;
  46.             String FROM = "ingeniero.desarrollo011@smarttmt.com";
  47.             final String username = "ingeniero.desarrollo011@smarttmt.com";
  48.             final String password = "Ingeniero159263";
  49.            
  50.             Properties props = NEW Properties();
  51.             props.put("mail.transport.protocol", "smtp");
  52.             props.put("mail.smtp.auth", "true");
  53.             props.put("mail.smtp.starttls.enable", "true");
  54.             props.put("mail.smtp.host", "smtp.gmail.com");
  55.             props.put("mail.smtps.auth", "false");
  56.             props.put("mail.debug", "true");
  57.             props.put("mail.smtp.port", "587");
  58.      
  59.             SESSION SESSION = SESSION.getInstance(props,
  60.                     NEW javax.mail.Authenticator() {
  61.                         protected PasswordAuthentication getPasswordAuthentication() {
  62.                             RETURN NEW PasswordAuthentication(username, password);
  63.                         }
  64.                     });
  65.            
  66.             //log de la sencion abierta de correos
  67.             SESSION.setDebug(TRUE);
  68.  
  69.             Message message = NEW MimeMessage(SESSION);
  70.             //cabecera del mensaje
  71.             message.setHeader("X-Mailer", "smtpssend");
  72.             //prioridad del mensaje - cabecera
  73.             message.addHeader("X-Priority", "1");
  74.             message.setFrom(NEW InternetAddress(FROM));
  75.             message.setRecipients(Message.RecipientType.TO,
  76.                     InternetAddress.parse(TO));
  77.             message.setSubject(FacesUtils.getMessageFromBundle(PropertiesApp.FILES_MESSAGES.PROPERTIES, "bean.sendMail.subject"));
  78.             BufferedReader br = NEW BufferedReader(NEW FileReader(ctx.getRealPath("/") + "/WEB-INF/includes/emailTemplates/" + "activarUsuario" + ".html"));
  79.             try {
  80.                 sb = NEW StringBuilder();
  81.                 String line = br.readLine();
  82.                 while (line != NULL) {
  83.                     String linea = line.TRIM().toString();
  84.                     sb.append(line);
  85.                     sb.append('\n');
  86.                     line = br.readLine();
  87.                 }
  88.                 String everything = sb.toString();
  89.             } finally {
  90.                 br.close();
  91.             }
  92.             String aux = sb.toString();
  93.             //aux = aux.REPLACE("XzUyM", KEY);
  94.             message.setContent(aux, "text/html; charset=utf-8");
  95.            
  96.             //Para enviar el mensaje usamos la clase Transport, que se obtiene de SESSION.
  97.             Transport myTransport = (Transport) SESSION.getTransport("smtp");
  98.             //se establece la conexión, dando el nombre de usuario y password.
  99.             myTransport.CONNECT();
  100.            
  101.             //Se envia el email
  102.             myTransport.sendMessage(message, message.getAllRecipients());
  103.             myTransport.close();
  104.            
  105.             //message.setText(contentMail);
  106.             ///Transport.send(message);
  107.         } catch (Exception exception) {
  108.             throw NEW RuntimeException(exception);
  109.         }
  110.     }
  111.  
  112.     public void sendMessage(String subject, String titulo, String contenido, String pie, String url, String TO, String fileName, String KEY, String entity, EntityManagerFactory emf) throws Exception {
  113.         BOOLEAN encontrado = FALSE;
  114.         Object[] correoObj;
  115.         try {
  116.             SessionController sessionController = (SessionController) FacesUtils.getManagedBean(com.smarttmt.suite.ejb.DOMAIN.Constant.CONTROLADOR_SESION+com.smarttmt.suite.ejb.DOMAIN.Constant.BEAN);
  117.             correoObj = sessionController.getEntityEJB().getNativeQueryJpaController().getEmpresaDatos();
  118.             StringBuilder sb;
  119.             String FROM = "ingeniero.desarrollo011@smarttmt.com";
  120.             final String username = "ingeniero.desarrollo011@smarttmt.com";
  121.             final String password = "Ingeniero159263";
  122.            
  123.             Properties props = NEW Properties();
  124.             props.put("mail.transport.protocol", "smtp");
  125.             props.put("mail.smtp.auth", "true");
  126.             props.put("mail.smtp.starttls.enable", "true");
  127.             props.put("mail.smtp.host", "smtp.gmail.com");
  128.             props.put("mail.smtps.auth", "false");
  129.             props.put("mail.debug", "true");
  130.             props.put("mail.smtp.port", "587");
  131.             SESSION SESSION = SESSION.getInstance(props,
  132.                     NEW javax.mail.Authenticator() {
  133.                         protected PasswordAuthentication getPasswordAuthentication() {
  134.                             RETURN NEW PasswordAuthentication(username, password);
  135.                         }
  136.                     });
  137.             ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance()
  138.                     .getExternalContext().getContext();
  139.             Message message = NEW MimeMessage(SESSION);
  140.                        //log de la sencion abierta de correos
  141.             SESSION.setDebug(TRUE);
  142.  
  143.             //cabecera del mensaje
  144.             message.setHeader("X-Mailer", "smtpssend");
  145.             //prioridad del mensaje - cabecera
  146.             message.addHeader("X-Priority", "1");
  147.            
  148.             message.setFrom(NEW InternetAddress(FROM));
  149.             message.setRecipients(Message.RecipientType.TO,
  150.                     InternetAddress.parse(TO));
  151.             message.setSubject(subject);
  152.             BufferedReader br = NEW BufferedReader(NEW FileReader(ctx.getRealPath("/") + "/WEB-INF/includes/emailTemplates/" + fileName + ".html"));
  153.  
  154.             String imagen_encabezado = FacesUtils.getMessageFromBundle(PropertiesApp.FILES_MESSAGES.PROPERTIES, "bean.sendMail.logoHead");
  155.             String pie_prueba = FacesUtils.getMessageFromBundle(PropertiesApp.FILES_MESSAGES.PROPERTIES, "bean.sendMail.logoContent");
  156.             String imagen_pie = FacesUtils.getMessageFromBundle(PropertiesApp.FILES_MESSAGES.PROPERTIES, "bean.sendMail.logoPie");
  157.  
  158.             try {
  159.                 sb = NEW StringBuilder();
  160.                 String line = br.readLine();
  161.                 while (line != NULL) {
  162.                     String linea = line.TRIM();
  163.                     IF (linea.contains("*****&&LOGO_&&****")) {
  164.                         String REPLACE = line.REPLACE("*****&&LOGO_&&****", imagen_encabezado);
  165.                         sb.append(REPLACE);
  166.                         sb.append('\n');
  167.                         line = br.readLine();
  168.                         encontrado = TRUE;
  169.                     }
  170.                     IF (linea.contains("*****&&BANER_&&****")) {
  171.                         String REPLACE = line.REPLACE("*****&&BANER_&&****", pie_prueba);
  172.                         sb.append(REPLACE);
  173.                         sb.append('\n');
  174.                         line = br.readLine();
  175.                         encontrado = TRUE;
  176.                     }
  177.                     IF (linea.equalsIgnoreCase("*****&&TITULO_&&*****")) {
  178.                         String REPLACE = line.REPLACE(line, titulo);
  179.                         sb.append(REPLACE);
  180.                         sb.append('\n');
  181.                         line = br.readLine();
  182.                         encontrado = TRUE;
  183.                     }
  184.                     IF (linea.equalsIgnoreCase("*****&&CONTENIDO_&&*****")) {
  185.                         String REPLACE = line.REPLACE(line, contenido);
  186.                         sb.append(REPLACE);
  187.                         sb.append('\n');
  188.                         line = br.readLine();
  189.                         encontrado = TRUE;
  190.                     }
  191.                     IF (linea.equalsIgnoreCase("*****&&PIE_&&*****")) {
  192.                         String REPLACE = line.REPLACE(line, pie);
  193.                         sb.append(REPLACE);
  194.                         sb.append('\n');
  195.                         line = br.readLine();
  196.                         encontrado = TRUE;
  197.                     }
  198.                     IF (linea.contains("*****&&URL_&&*****")) {
  199.                         String REPLACE = line.REPLACE("*****&&URL_&&*****", url);
  200.                         sb.append(REPLACE);
  201.                         sb.append('\n');
  202.                         line = br.readLine();
  203.                         encontrado = TRUE;
  204.                     }
  205.                     IF (linea.contains("*****&&LOGOPIE_&&****")) {
  206.                         String REPLACE = line.REPLACE("*****&&LOGOPIE_&&****", imagen_pie);
  207.                         sb.append(REPLACE);
  208.                         sb.append('\n');
  209.                         line = br.readLine();
  210.                         encontrado = TRUE;
  211.                     }
  212.                     IF (!encontrado) {
  213.                         sb.append(line);
  214.                         sb.append('\n');
  215.                         line = br.readLine();
  216.                         encontrado = FALSE;
  217.                     }
  218.                     encontrado = FALSE;
  219.                 }
  220.                 String everything = sb.toString();
  221.             } finally {
  222.                 br.close();
  223.             }
  224.             String aux = sb.toString();
  225.             aux = aux.REPLACE("XzUyM", KEY);
  226.             aux = aux.REPLACE("WcxMer", (entity != NULL ? entity : ((ApplicationController) FacesUtils.getManagedBean("applicationControllerBean")).getEntity()));
  227.             message.setContent(aux, "text/html; charset=utf-8");
  228.            
  229.            
  230.             //Para enviar el mensaje usamos la clase Transport, que se obtiene de SESSION.
  231.             Transport myTransport = (Transport) SESSION.getTransport("smtp");
  232.             //se establece la conexión, dando el nombre de usuario y password.
  233.             myTransport.CONNECT();
  234.            
  235.             //Se envia el email
  236.             myTransport.sendMessage(message, message.getAllRecipients());
  237.             myTransport.close();
  238.                  
  239.             //Transport.send(message);
  240.         } catch (Exception exception) {
  241.             throw NEW RuntimeException(exception);
  242.         }
  243.     }
  244.  
  245.     public SESSION getSession() {
  246.         Authenticator authenticator = NEW Authenticator();
  247.         Properties properties = NEW Properties();
  248.         properties.put("mail.transport.protocol", "smtp");
  249.         properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
  250.         properties.setProperty("mail.smtp.auth", "true");
  251.         properties.setProperty("mail.smtp.host", "smtp.gmail.com");
  252.         properties.setProperty("mail.smtp.port", "587");
  253.         RETURN SESSION.getInstance(properties, authenticator);
  254.     }
  255.  
  256.     private class Authenticator extends javax.mail.Authenticator {
  257.  
  258.         private PasswordAuthentication authentication;
  259.  
  260.         public Authenticator() {
  261.             final String username = "ingeniero.desarrollo011@smarttmt.com";
  262.             final String password = "Ingeniero159263";
  263.             authentication = NEW PasswordAuthentication(username, password);
  264.         }
  265.  
  266.         protected PasswordAuthentication getPasswordAuthentication() {
  267.             RETURN authentication;
  268.         }
  269.     }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement