Created
May 8, 2015 21:35
-
-
Save devniel/69cd128ad8ae0bd2763c to your computer and use it in GitHub Desktop.
Envío de correo Gmail con Java Mail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package pe.xxx.processor.util; | |
| import java.util.List; | |
| import java.util.Properties; | |
| import javax.mail.Address; | |
| import javax.mail.Message; | |
| import javax.mail.MessagingException; | |
| import javax.mail.PasswordAuthentication; | |
| import javax.mail.Session; | |
| import javax.mail.Transport; | |
| import javax.mail.internet.InternetAddress; | |
| import javax.mail.internet.MimeMessage; | |
| import pe.xxx.processor.util.Utilitario; | |
| public class Correo { | |
| /** | |
| * Tipo de correo a través del cual | |
| * se determina el mensaje de notificación a | |
| * enviar. | |
| **/ | |
| public enum Tipo { | |
| GENERACION, | |
| PREGENERACION | |
| } | |
| /** | |
| * Método que envía un correo simple | |
| * @param listaMail | |
| * @param asunto | |
| * @param mensaje | |
| * @throws Exception | |
| */ | |
| public static void enviar(List<String> listaMail, String asunto, String mensaje) throws Exception { | |
| final String HOST_MAIL = Utilitario.getProperty("host_mail"); | |
| final String PORT_MAIL = Utilitario.getProperty("port_mail"); | |
| final String USER_MAIL = Utilitario.getProperty("user_mail"); | |
| final String PWD_MAIL = Utilitario.getProperty("pwd_mail"); | |
| System.out.println("host mail : " + HOST_MAIL); | |
| System.out.println("port mail : " + PORT_MAIL); | |
| System.out.println("user mail : " + USER_MAIL); | |
| System.out.println("pwd mail : " + PWD_MAIL); | |
| Properties props = new Properties(); | |
| props.put("mail.smtp.auth", "true"); | |
| props.put("mail.smtp.starttls.enable", "true"); | |
| props.put("mail.smtp.port", "465"); | |
| props.put("mail.smtp.host", "smtp.gmail.com"); | |
| props.put("mail.smtp.user", "[email protected]"); | |
| props.put("mail.smtp.pass", "xxx"); | |
| props.put("mail.smtp.timeout", 1000); | |
| props.put("mail.smtp.socketFactory.port", "465"); | |
| props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); | |
| props.put("mail.smtp.socketFactory.fallback", "false"); | |
| Session session = Session.getInstance(props, | |
| new javax.mail.Authenticator() { | |
| protected PasswordAuthentication getPasswordAuthentication() { | |
| return new PasswordAuthentication("[email protected]", "xxx"); | |
| } | |
| }); | |
| session.setDebug(true); | |
| try{ | |
| Message message = new MimeMessage(session); | |
| message.setFrom(new InternetAddress("[email protected]")); | |
| Address[] recipients = new InternetAddress[listaMail.size()]; | |
| for(int i=0; i < listaMail.size(); i++){ | |
| recipients[i] = new InternetAddress(listaMail.get(i)); | |
| } | |
| message.setRecipients(Message.RecipientType.TO, recipients); | |
| message.setSubject(asunto); | |
| message.setText(mensaje); | |
| System.out.println("************ ENVIANDO CORREO ***************"); | |
| Transport transport = session.getTransport("smtps"); | |
| transport.connect("smtp.gmail.com", 465, "[email protected]", "xxx"); | |
| Transport.send(message); | |
| transport.close(); | |
| System.out.println("************ CORREO ENVIADO ***************"); | |
| }catch(Exception e){ | |
| System.out.println("************ ERROR EN ENVÍO DE CORREO ***************"); | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Notifica el término de una pregeneración o generación | |
| * de recibos con un formato específico de correo. | |
| * | |
| * @param listaMail | |
| * @param tipo | |
| * @param numeroPreGeneracion | |
| * @throws Exception | |
| */ | |
| public static void notificar(List<String> listaMail, Tipo tipo, String numeroPreGeneracion) throws Exception{ | |
| switch(tipo){ | |
| case GENERACION : | |
| Correo.enviar( | |
| listaMail, | |
| "XXX INTRANET - Generación " + numeroPreGeneracion + " de recibo concluída", | |
| "La orden " + numeroPreGeneracion + " de generación de recibos ha concluído satisfactoriamente." | |
| ); | |
| break; | |
| case PREGENERACION : | |
| Correo.enviar(listaMail, | |
| "XXX INTRANET - PreGeneración " + numeroPreGeneracion + " de recibo concluída", | |
| "La orden " + numeroPreGeneracion + " de pregeneración de recibos ha concluído satisfactoriamente." | |
| ); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment