123 lines
4.5 KiB
Java
123 lines
4.5 KiB
Java
package com.tarisan.control;
|
|
|
|
import java.io.File;
|
|
import java.util.Properties;
|
|
|
|
import javax.activation.DataHandler;
|
|
// import javax.activation.DataSource;
|
|
import javax.activation.FileDataSource;
|
|
import jakarta.mail.BodyPart;
|
|
import jakarta.mail.Message;
|
|
import jakarta.mail.Multipart;
|
|
import jakarta.mail.Session;
|
|
import jakarta.mail.Transport;
|
|
import jakarta.mail.internet.InternetAddress;
|
|
import jakarta.mail.internet.MimeBodyPart;
|
|
import jakarta.mail.internet.MimeMessage;
|
|
import jakarta.mail.internet.MimeMultipart;
|
|
|
|
import com.tarisan.log.LogTarisan;
|
|
import com.tarisan.log.NivelLog;
|
|
// import com.sun.mail.util.MessageRemovedIOException;
|
|
|
|
public class JavaMail {
|
|
|
|
public static void main(String[] args) {
|
|
/*JavaMail javaMail = new JavaMail();
|
|
javaMail.mandarCorreo(mensaje,tarjeta); */
|
|
}
|
|
|
|
public void mandarCorreo(String mensaje, String tarjeta, String email, String asunto, String nombreAdjuntos) {
|
|
LogTarisan.logger.log(NivelLog.DEBUG, "com.tarisan.servlets/JavaMail -- mandarCorreo("+mensaje+","+tarjeta+","+email+","+asunto+","+nombreAdjuntos);
|
|
|
|
// El correo de envío
|
|
String correoEnvia = "sistemas@imqnavarra.com";
|
|
String claveCorreo = "s1st3masacc17";
|
|
|
|
// La configuración para enviar correo
|
|
Properties properties = new Properties();
|
|
properties.put("mail.smtp.host", "smtp.imqnavarra.com");
|
|
properties.put("mail.smtp.starttls.enable", "true");
|
|
properties.put("mail.smtp.port", "587");
|
|
properties.put("mail.smtp.auth", "true");
|
|
properties.put("mail.user", correoEnvia);
|
|
properties.put("mail.password", claveCorreo);
|
|
properties.put("mail.debug", "true"); //para que nos muestre en detalle el proceso
|
|
|
|
// Obtener la sesion
|
|
Session session = Session.getInstance(properties, null);
|
|
|
|
try {
|
|
// Crear el cuerpo del mensaje
|
|
MimeMessage mimeMessage = new MimeMessage(session);
|
|
|
|
// Agregar quien envía el correo
|
|
//mimeMessage.setFrom(new InternetAddress(correoEnvia, "sistemas"));
|
|
mimeMessage.setFrom(new InternetAddress(email));
|
|
|
|
// Los destinatarios
|
|
InternetAddress[] internetAddresses = {
|
|
new InternetAddress("soporte_tarisan@imqnavarra.com")};/*,
|
|
new InternetAddress("david.andueza@imqnavarra.com"),
|
|
new InternetAddress("sistemas@imqnavarra.com") };*/
|
|
|
|
// Agregar los destinatarios al mensaje
|
|
mimeMessage.setRecipients(Message.RecipientType.TO,
|
|
internetAddresses);
|
|
|
|
// Agregar el asunto al correo
|
|
mimeMessage.setSubject(asunto);
|
|
|
|
// Creo la parte del mensaje
|
|
String mensajeCompleto = "";
|
|
if (tarjeta!=null && tarjeta.compareTo("")!=0){
|
|
tarjeta = tarjeta.replace("()", "%");
|
|
mensajeCompleto = mensaje+"\n\n\nTARJETA: "+tarjeta;
|
|
}else{
|
|
mensajeCompleto = mensaje;
|
|
}
|
|
|
|
MimeBodyPart mimeBodyPart = new MimeBodyPart();
|
|
mimeBodyPart.setText(mensajeCompleto);
|
|
|
|
// Crear el multipart para agregar la parte del mensaje anterior
|
|
Multipart multipart = new MimeMultipart();
|
|
multipart.addBodyPart(mimeBodyPart);
|
|
|
|
// Creo la parte del archivo adjunto
|
|
BodyPart adjuntoBodyPart = new MimeBodyPart();
|
|
|
|
if( nombreAdjuntos!=null && nombreAdjuntos.length()>0 ){
|
|
String[] partes = nombreAdjuntos.split("--");
|
|
for (int x=0; x < partes.length ; x++) {
|
|
String rutaAdjunto = partes[x];
|
|
adjuntoBodyPart = new MimeBodyPart();
|
|
LogTarisan.logger.log(NivelLog.DEBUG, "Ruta de adjunto "+x+": " + ParametrosConfiguracion.ruta_adjuntos+rutaAdjunto);
|
|
File f = new File(ParametrosConfiguracion.ruta_adjuntos+rutaAdjunto);
|
|
if( f.exists() ){
|
|
adjuntoBodyPart.setDataHandler(new DataHandler(new FileDataSource(ParametrosConfiguracion.ruta_adjuntos+"/"+rutaAdjunto)));
|
|
adjuntoBodyPart.setFileName( f.getName() );
|
|
multipart.addBodyPart(adjuntoBodyPart);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Agregar el multipart al cuerpo del mensaje
|
|
mimeMessage.setContent(multipart);
|
|
|
|
// Enviar el mensaje
|
|
Transport transport = session.getTransport("smtp");
|
|
transport.connect(correoEnvia, claveCorreo);
|
|
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
|
|
transport.close();
|
|
|
|
} catch (Exception ex) {
|
|
LogTarisan.logger.log(NivelLog.ERROR, "Error al Enviar el Correo");
|
|
ex.printStackTrace();
|
|
}
|
|
LogTarisan.logger.log(NivelLog.DEBUG, "Correo Enviado");
|
|
}
|
|
|
|
|
|
}
|