【问题标题】:How to include a html email with images in Servlet & send如何在 Servlet 中包含带有图像的 html 电子邮件并发送
【发布时间】:2016-08-15 13:58:57
【问题描述】:

我有一个处理用户注册的 servlet。此 servlet 还会向用户发送一封激活电子邮件。我已经在另一个类中实现了邮件发送,但这里是处理向用户发送邮件的函数:

if(localEjb.createUser(usr) != null)
        {
            boolean status = true;

            EmailSetting emailSetting = localEjb.fetchSetting();

            if(emailSetting != null)
            {
                Mail mail = new Mail();

                String message = "Dear " + lastName + " , "  +
                        "Your account has been created on Eventry. " +
                        "Please click this link '" + emailSetting.getBaseURL() + "activate?" + securityCode + "' to complete the activation process";

                status = mail.send(emailSetting, emailSetting.getRecipient(), email, Message.EVENTRY_ACCOUNT_ACTIVATION_INSTRUCTIONS, message, null, null);
            }

            /*this user has been successfully created on the portal*/
            this.respond(response, status, status ? Message.activation_mail_sent : Message.activation_mail_failed,null);
        }
        else
        {
            // a technical error occureed, we couldn't create the user (Should never happen)
            this.respond(response, true, Message.registration_failed + Message.technical_fault, null);
        }

现在,如您所见,我已经以一种非常简单的方式编写了要发送的消息,并且可以正常工作。但我现在真正需要的是拥有一个包含 CSS 和 HTML 的自定义电子邮件。我已经有了这个,但我不知道如何将它插入到 servlet 中。或者如果我可以使用JSP,我应该怎么做?

谢谢。

【问题讨论】:

  • Google for java send html email with images,确实有很多的示例和教程。
  • 感谢您的回答,但挑战不在于将 HTML 放入消息中,而在于 CSS 是问题所在。谢谢。 P.s 我认为在任何人对我的问题投反对票之前,他们至少应该就我能做什么提出建议。
  • 嗯,此类搜索的大部分结果不仅讨论如何发送 HTML 电子邮件,还讨论如何将其他对象(图像、CSS 等)嵌入此类 HTML。

标签: java html jsp email servlets


【解决方案1】:

您可以将forward 传递给JSP,并提供HttpServletResponseWrapper 以将JSP 的ServletResponse.getWriter 替换为StringWriter。

但是,电子邮件具有需要测试的特定属性,例如 HTML 的外观。电子邮件必须是 text/html 内容类型,最好回退到 text/plain。图片必须随邮件一起提供。 CSS 仍然是一个招标课题。

所以我会把任务分成

  • 用图像制作一个简单的 HTML 并发送该 HTML
  • 制作一个填充占位符的 JSP
  • 制作更复杂的 HTML,实现所需的布局
  • 在多个电子邮件客户端(Web、Outlook、应用程序)中进行测试

【讨论】:

    猜你喜欢
    • 2018-05-05
    • 2012-04-07
    • 2013-11-11
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    相关资源
    最近更新 更多