【问题标题】:How do I send HTML email in Spring MVC?如何在 Spring MVC 中发送 HTML 电子邮件?
【发布时间】:2011-07-14 11:23:58
【问题描述】:

我已经使用这个成功发送了简单的电子邮件:

SimpleMailMessage mailMessage = new SimpleMailMessage();

mailMessage.setTo("someone@abc.com");
mailMessage.setSubject("This is the test message for testing gmail smtp server using spring mail");
mailMessage.setFrom("abc@gmail.com");
mailMessage.setText("This is the test message for testing gmail smtp server using spring mail. \n" +
        "Thanks \n Regards \n Saurabh ");
mailSender.send(mailMessage);

我需要更改什么设置才能发送 html 电子邮件

【问题讨论】:

  • 这里还有一篇关于如何使用 spring 发送电子邮件的帖子。它对电子邮件的 HTML 内容使用速度模板,在示例中它使用 Gmail 发送电子邮件。但我认为您可以配置任何邮件服务器供您使用。它也有附件示例。 Send Email with Spring Using Velocity Template.

标签: java spring-mvc


【解决方案1】:
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.MimeMessageHelper;

MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");
String htmlMsg = "<h3>Hello World!</h3>";
//mimeMessage.setContent(htmlMsg, "text/html"); /** Use this or below line **/
helper.setText(htmlMsg, true); // Use this or above line.
helper.setTo("someone@abc.com");
helper.setSubject("This is the test message for testing gmail smtp server using spring mail");
helper.setFrom("abc@gmail.com");
mailSender.send(mimeMessage);

【讨论】:

  • 谢谢。你的回答有帮助。我发现您也可以使用helper.setText(htmlMsg, true);,其中真正的标志表明包含的文本是 HTML。它将为 HTML 邮件应用内容类型“text/html”,因此您无需填写 mimeMessage.setContent(htmlMsg, "text/html");
  • 如果您想为电子邮件客户提供这两种选择,也可以致电helper.setText(plainText, htmlText)
【解决方案2】:

在 Spring 中应该这样做:

您的电子邮件类:

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="youremail@gmail.com" />
        <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>

用法:

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("sender@gmail.com",
            "receiver@gmail.com",
            "test html email",
            html);

完整示例here

【讨论】:

    【解决方案3】:

    我认为 SimpleMailMessage 类没有这样的选项。

    我相信您可以使用 JavaMailSender 和 MimeMessagePreparator 来完成,因为您需要为 HTML 设置 MIME 内容类型。

    查看此链接获取帮助:

    http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mail.html

    【讨论】:

      【解决方案4】:

      您可能有兴趣查看这篇文章:“Thymeleaf 在 Spring 中的丰富 HTML 电子邮件” http://www.thymeleaf.org/doc/articles/springmail.html

      它使用 Thymeleaf 作为模板视图层,但其中解释的概念和特定于 Spring 的代码对所有 Spring 应用程序都是通用的。

      此外,它还有一个配套的示例应用程序,您可以将其源代码用作满足您需求的基础。

      问候, 丹尼尔。

      【讨论】:

        【解决方案5】:

        班级等级:

        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;
        
        }
        

        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>
        

        【讨论】:

          【解决方案6】:
          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("abc@gmail.com");
                      helper.setFrom("xyz@gmail.com");
                      helper.setSubject("new email");
                      helper.setText(text, true);         
                      mailSender.send(message);
          

          【讨论】:

            猜你喜欢
            • 2016-11-06
            • 1970-01-01
            • 2011-01-01
            • 2023-03-24
            • 1970-01-01
            • 1970-01-01
            • 2012-08-27
            相关资源
            最近更新 更多