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