Si usas Spring configura tu config.properties.
mail.smtpHost=127.0.0.1
mail.smtpPort=25
mail.smtpUsername=userSMT9
mail.smtpPassword=12345
mail.charSet=ISO-8859-1
Opcionalmente si quieres evitarte la carga del properties puedes configurar el application-context.xml
<bean id="mailUtil" class=" com.util.MailUtil">
<property
name="useSSL"
value="${mail.useSSL}" />
<property
name="smtpHost"
value="${mail.smtpHost}" />
<property
name="smtpPort"
value="${mail.smtpPort}" />
<property
name="smtpUsername"
value="${mail.smtpUsername}" />
<property
name="smtpPassword"
value="${mail.smtpPassword}" />
</bean>
//Esta de abajo es la clase
package com.util;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@SuppressWarnings("unchecked")
public class MailUtil {
private boolean useSSL = false;
private String smtpHost;
private int smtpPort = 25;
private String smtpUsername;
private String smtpPassword;
private String charSet = "ISO-8859-1";
public boolean isUseSSL() {
return useSSL;
}
public void setUseSSL(boolean useSSL) {
this.useSSL = useSSL;
}
public String getSmtpHost() {
return smtpHost;
}
public void setSmtpHost(String smtpHost) {
this.smtpHost = smtpHost;
}
public int getSmtpPort() {
return smtpPort;
}
public void setSmtpPort(int smtpPort) {
this.smtpPort = smtpPort;
}
public String getSmtpUsername() {
return smtpUsername;
}
public void setSmtpUsername(String smtpUsername) {
this.smtpUsername = smtpUsername;
}
public String getSmtpPassword() {
return smtpPassword;
}
public void setSmtpPassword(String smtpPassword) {
this.smtpPassword = smtpPassword;
}
public String getCharSet() {
return charSet;
}
public void setCharSet(String charSet) {
this.charSet = charSet;
}
public void sendMail(String from,String to, String html,String subject,HashMap mapaImagenes){
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost(this.smtpHost);
sender.setPort(this.smtpPort);
sender.setUsername(this.smtpUsername);
sender.setPassword(this.smtpPassword);
MimeMessage message = sender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(html,true);
Iterator it = mapaImagenes.entrySet().iterator();
while (it.hasNext()) {
Map.Entry e = (Map.Entry)it.next();
Resource resource=new ClassPathResource(e.getValue().toString());
helper.addInline(e.getKey().toString(), resource);
}
sender.send(message);
} catch (MessagingException e) {
// TODO Auto-generated catch block agregar log
e.printStackTrace();
}
};
}
La forma de uso sería así.
//Forma de uso
HashMap mapaIdentificador = new HashMap();
mapaIdentificador.put("identifier1234","resources/mail/header.jpg");// la imagen esta en el package resources
MailUtil mailUtil= new MailUtil();
mailUtil.sendMail("administrador@lafabricadesoftware.com","usuario@lafabricadesoftware.com",
"<html><body><img src='cid:identifier1234'></body></html>", "hola Mundo", mapaIdentificador);
No hay comentarios:
Publicar un comentario