tutorial mvc mkyong mail javamailsender example enviar correo java spring-mvc

java - mkyong - ¿Cómo envío un correo electrónico HTML en Spring MVC?



spring mail properties (6)

He enviado correos electrónicos simples usando esto:

SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setTo("[email protected]"); mailMessage.setSubject("This is the test message for testing gmail smtp server using spring mail"); mailMessage.setFrom("[email protected]"); mailMessage.setText("This is the test message for testing gmail smtp server using spring mail. /n" + "Thanks /n Regards /n Saurabh "); mailSender.send(mailMessage);

Qué configuración necesito resolver para poder enviar correos electrónicos html


En primavera esto debería hacerse de esta manera:

Tu clase de correo electrónico:

import java.util.logging.Level; import java.util.logging.Logger; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; public class HTMLMail { private JavaMailSender mailSender; public void setMailSender(JavaMailSender mailSender) { this.mailSender = mailSender; } public void sendMail(String from, String to, String subject, String msg) { try { MimeMessage message = mailSender.createMimeMessage(); message.setSubject(subject); MimeMessageHelper helper; helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setText(msg, true); mailSender.send(message); } catch (MessagingException ex) { Logger.getLogger(HTMLMail.class.getName()).log(Level.SEVERE, null, ex); } } }

beans: (Spring-Mail.xml)

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.gmail.com" /> <property name="port" value="587" /> <property name="username" value="[email protected]" /> <property name="password" value="yourpassword" /> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.starttls.enable">true</prop> </props> </property> </bean> <bean id="htmlMail" class="com.mohi.common.HTMLMail"> <property name="mailSender" ref="mailSender" /> </bean> </beans>

Uso:

ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Mail.xml"); HTMLMail mm = (HTMLMail) context.getBean("htmlMail"); String html="<p>Hi!</p><a href=/"google.com/">Link text</a>"; mm.sendMail("[email protected]", "[email protected]", "test html email", html);

Ejemplo completo here .



Puede que le interese consultar este artículo: "Correo electrónico HTML enriquecido en Spring with Thymeleaf" http://www.thymeleaf.org/doc/articles/springmail.html

Utiliza Thymeleaf como una capa de vista de plantillas, pero los conceptos y el código específico de Spring explicados allí son comunes a todas las aplicaciones de Spring.

Además, tiene una aplicación complementaria de ejemplo, cuyo código fuente puede usar como base para sus necesidades.

Saludos, Daniel.


Nivel de clase:

public String sendEmailToUsers(String emailId,String subject, String name){ String result =null; MimeMessage message =mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, false, "utf-8"); String htmlMsg = "<body style=''border:2px solid black''>" +"Your onetime password for registration is " + "Please use this OTP to complete your new user registration."+ "OTP is confidential, do not share this with anyone.</body>"; message.setContent(htmlMsg, "text/html"); helper.setTo(emailId); helper.setSubject(subject); result="success"; mailSender.send(message); } catch (MessagingException e) { throw new MailParseException(e); }finally { if(result !="success"){ result="fail"; } } return result; }

Nivel de XML:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.gmail.com" /> <property name="port" value="587" /> <property name="username" value="********@gmail.com" /> <property name="password" value="********" /> <property name="javaMailProperties"> <props> <prop key="mail.transport.protocol">smtp</prop> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.starttls.enable">true</prop> </props> </property> </bean>


import javax.mail.internet.MimeMessage; import org.springframework.mail.javamail.MimeMessageHelper; MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, "utf-8"); String htmlMsg = "<h3>Hello World!</h3>"; mimeMessage.setContent(htmlMsg, "text/html"); helper.setTo("[email protected]"); helper.setSubject("This is the test message for testing gmail smtp server using spring mail"); helper.setFrom("[email protected]"); mailSender.send(mimeMessage);


String emailMessage = report.toString(); Map velocityContext = new HashMap(); velocityContext.put("firstName", "messi"); velocityContext.put("Date",date ); velocityContext.put("Exception",emailMessage ); String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "VelocityTemplate.vm","UTF-8", velocityContext); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper; helper = new MimeMessageHelper(message, true); helper.setTo("[email protected]"); helper.setFrom("[email protected]"); helper.setSubject("new email"); helper.setText(text, true); mailSender.send(message);