Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.smarttmt.suite.web.utilities;
- import com.smarttmt.suite.web.beans.ApplicationController;
- import com.smarttmt.suite.web.beans.SessionController;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import javax.enterprise.context.ApplicationScoped;
- import javax.faces.context.FacesContext;
- import javax.inject.Named;
- import javax.servlet.ServletContext;
- import javax.mail.Message;
- import javax.mail.PasswordAuthentication;
- import javax.mail.SESSION;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import java.util.*;
- import javax.persistence.EntityManagerFactory;
- /**
- *
- * @author Nicolás Rubén Caballero Ortega (helaman58@gmail.com)
- */
- @Named(VALUE = "sendEmail")
- @ApplicationScoped
- public class SendEmail {
- public SendEmail() {
- }
- /*
- Metodo para el envio de correo.
- */
- public void sendMessage(String contentMail) throws Exception {
- try {
- //String TO = "leydirodriguezz@gmail.com";
- String TO = "ingeniero.desarrollo011@smarttmt.com";
- StringBuilder sb;
- ServletContext ctx = NULL;
- String FROM = "ingeniero.desarrollo011@smarttmt.com";
- final String username = "ingeniero.desarrollo011@smarttmt.com";
- final String password = "Ingeniero159263";
- Properties props = NEW Properties();
- props.put("mail.transport.protocol", "smtp");
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtps.auth", "false");
- props.put("mail.debug", "true");
- props.put("mail.smtp.port", "587");
- SESSION SESSION = SESSION.getInstance(props,
- NEW javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- RETURN NEW PasswordAuthentication(username, password);
- }
- });
- //log de la sencion abierta de correos
- SESSION.setDebug(TRUE);
- Message message = NEW MimeMessage(SESSION);
- //cabecera del mensaje
- message.setHeader("X-Mailer", "smtpssend");
- //prioridad del mensaje - cabecera
- message.addHeader("X-Priority", "1");
- message.setFrom(NEW InternetAddress(FROM));
- message.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse(TO));
- message.setSubject(FacesUtils.getMessageFromBundle(PropertiesApp.FILES_MESSAGES.PROPERTIES, "bean.sendMail.subject"));
- BufferedReader br = NEW BufferedReader(NEW FileReader(ctx.getRealPath("/") + "/WEB-INF/includes/emailTemplates/" + "activarUsuario" + ".html"));
- try {
- sb = NEW StringBuilder();
- String line = br.readLine();
- while (line != NULL) {
- String linea = line.TRIM().toString();
- sb.append(line);
- sb.append('\n');
- line = br.readLine();
- }
- String everything = sb.toString();
- } finally {
- br.close();
- }
- String aux = sb.toString();
- //aux = aux.REPLACE("XzUyM", KEY);
- message.setContent(aux, "text/html; charset=utf-8");
- //Para enviar el mensaje usamos la clase Transport, que se obtiene de SESSION.
- Transport myTransport = (Transport) SESSION.getTransport("smtp");
- //se establece la conexión, dando el nombre de usuario y password.
- myTransport.CONNECT();
- //Se envia el email
- myTransport.sendMessage(message, message.getAllRecipients());
- myTransport.close();
- //message.setText(contentMail);
- ///Transport.send(message);
- } catch (Exception exception) {
- throw NEW RuntimeException(exception);
- }
- }
- 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 {
- BOOLEAN encontrado = FALSE;
- Object[] correoObj;
- try {
- SessionController sessionController = (SessionController) FacesUtils.getManagedBean(com.smarttmt.suite.ejb.DOMAIN.Constant.CONTROLADOR_SESION+com.smarttmt.suite.ejb.DOMAIN.Constant.BEAN);
- correoObj = sessionController.getEntityEJB().getNativeQueryJpaController().getEmpresaDatos();
- StringBuilder sb;
- String FROM = "ingeniero.desarrollo011@smarttmt.com";
- final String username = "ingeniero.desarrollo011@smarttmt.com";
- final String password = "Ingeniero159263";
- Properties props = NEW Properties();
- props.put("mail.transport.protocol", "smtp");
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtps.auth", "false");
- props.put("mail.debug", "true");
- props.put("mail.smtp.port", "587");
- SESSION SESSION = SESSION.getInstance(props,
- NEW javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- RETURN NEW PasswordAuthentication(username, password);
- }
- });
- ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance()
- .getExternalContext().getContext();
- Message message = NEW MimeMessage(SESSION);
- //log de la sencion abierta de correos
- SESSION.setDebug(TRUE);
- //cabecera del mensaje
- message.setHeader("X-Mailer", "smtpssend");
- //prioridad del mensaje - cabecera
- message.addHeader("X-Priority", "1");
- message.setFrom(NEW InternetAddress(FROM));
- message.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse(TO));
- message.setSubject(subject);
- BufferedReader br = NEW BufferedReader(NEW FileReader(ctx.getRealPath("/") + "/WEB-INF/includes/emailTemplates/" + fileName + ".html"));
- String imagen_encabezado = FacesUtils.getMessageFromBundle(PropertiesApp.FILES_MESSAGES.PROPERTIES, "bean.sendMail.logoHead");
- String pie_prueba = FacesUtils.getMessageFromBundle(PropertiesApp.FILES_MESSAGES.PROPERTIES, "bean.sendMail.logoContent");
- String imagen_pie = FacesUtils.getMessageFromBundle(PropertiesApp.FILES_MESSAGES.PROPERTIES, "bean.sendMail.logoPie");
- try {
- sb = NEW StringBuilder();
- String line = br.readLine();
- while (line != NULL) {
- String linea = line.TRIM();
- IF (linea.contains("*****&&LOGO_&&****")) {
- String REPLACE = line.REPLACE("*****&&LOGO_&&****", imagen_encabezado);
- sb.append(REPLACE);
- sb.append('\n');
- line = br.readLine();
- encontrado = TRUE;
- }
- IF (linea.contains("*****&&BANER_&&****")) {
- String REPLACE = line.REPLACE("*****&&BANER_&&****", pie_prueba);
- sb.append(REPLACE);
- sb.append('\n');
- line = br.readLine();
- encontrado = TRUE;
- }
- IF (linea.equalsIgnoreCase("*****&&TITULO_&&*****")) {
- String REPLACE = line.REPLACE(line, titulo);
- sb.append(REPLACE);
- sb.append('\n');
- line = br.readLine();
- encontrado = TRUE;
- }
- IF (linea.equalsIgnoreCase("*****&&CONTENIDO_&&*****")) {
- String REPLACE = line.REPLACE(line, contenido);
- sb.append(REPLACE);
- sb.append('\n');
- line = br.readLine();
- encontrado = TRUE;
- }
- IF (linea.equalsIgnoreCase("*****&&PIE_&&*****")) {
- String REPLACE = line.REPLACE(line, pie);
- sb.append(REPLACE);
- sb.append('\n');
- line = br.readLine();
- encontrado = TRUE;
- }
- IF (linea.contains("*****&&URL_&&*****")) {
- String REPLACE = line.REPLACE("*****&&URL_&&*****", url);
- sb.append(REPLACE);
- sb.append('\n');
- line = br.readLine();
- encontrado = TRUE;
- }
- IF (linea.contains("*****&&LOGOPIE_&&****")) {
- String REPLACE = line.REPLACE("*****&&LOGOPIE_&&****", imagen_pie);
- sb.append(REPLACE);
- sb.append('\n');
- line = br.readLine();
- encontrado = TRUE;
- }
- IF (!encontrado) {
- sb.append(line);
- sb.append('\n');
- line = br.readLine();
- encontrado = FALSE;
- }
- encontrado = FALSE;
- }
- String everything = sb.toString();
- } finally {
- br.close();
- }
- String aux = sb.toString();
- aux = aux.REPLACE("XzUyM", KEY);
- aux = aux.REPLACE("WcxMer", (entity != NULL ? entity : ((ApplicationController) FacesUtils.getManagedBean("applicationControllerBean")).getEntity()));
- message.setContent(aux, "text/html; charset=utf-8");
- //Para enviar el mensaje usamos la clase Transport, que se obtiene de SESSION.
- Transport myTransport = (Transport) SESSION.getTransport("smtp");
- //se establece la conexión, dando el nombre de usuario y password.
- myTransport.CONNECT();
- //Se envia el email
- myTransport.sendMessage(message, message.getAllRecipients());
- myTransport.close();
- //Transport.send(message);
- } catch (Exception exception) {
- throw NEW RuntimeException(exception);
- }
- }
- public SESSION getSession() {
- Authenticator authenticator = NEW Authenticator();
- Properties properties = NEW Properties();
- properties.put("mail.transport.protocol", "smtp");
- properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
- properties.setProperty("mail.smtp.auth", "true");
- properties.setProperty("mail.smtp.host", "smtp.gmail.com");
- properties.setProperty("mail.smtp.port", "587");
- RETURN SESSION.getInstance(properties, authenticator);
- }
- private class Authenticator extends javax.mail.Authenticator {
- private PasswordAuthentication authentication;
- public Authenticator() {
- final String username = "ingeniero.desarrollo011@smarttmt.com";
- final String password = "Ingeniero159263";
- authentication = NEW PasswordAuthentication(username, password);
- }
- protected PasswordAuthentication getPasswordAuthentication() {
- RETURN authentication;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement