如何从Javaservlet发送带有变量的HTML电子邮件?

如何从Javaservlet发送带有变量的HTML电子邮件?,java,html,servlets,Java,Html,Servlets,我目前有一个用Java编码的服务器(一个AmazonEC2实例),它有许多servlet来做各种web服务的事情。到目前为止,我一直在使用以下代码发送邀请电子邮件: public void sendInvitationEmail(String nameFrom, String emailTo, String withID) { SendEmailRequest request = new SendEmailRequest().withSource("in

我目前有一个用Java编码的服务器(一个AmazonEC2实例),它有许多servlet来做各种web服务的事情。到目前为止,我一直在使用以下代码发送邀请电子邮件:

public void sendInvitationEmail(String nameFrom, String emailTo, String withID)
        {

            SendEmailRequest request = new SendEmailRequest().withSource("invitation@myserver.com");

            List<String> toAddresses = new ArrayList<String>();
            toAddresses.add(emailTo);
            Destination dest = new Destination().withToAddresses(toAddresses);
            request.setDestination(dest);

            Content subjContent = new Content().withData("My Service Invitation Email");
            Message msg = new Message().withSubject(subjContent);

            String textVer = nameFrom +" has invited you to try My Service.";
            String htmlVer = "<p>"+nameFrom+" has invited you to try My Service.</p>";
            // Include a body in both text and HTML formats
            Content textContent = new Content().withData(textVer);
            Content htmlContent = new Content().withData(htmlVer);
            Body body = new Body().withHtml(htmlContent).withText(textContent);
            msg.setBody(body);

            request.setMessage(msg);

            try {           
                ses.sendEmail(request);
            }catch (AmazonServiceException ase) {
                handleExceptions(ase);
            } catch (AmazonClientException ace) {
                handleExceptions(ace);  
            }
        }
public void sendInvitationEmail(字符串nameFrom、字符串emailTo、字符串withID)
{
SendEmailRequest=新建SendEmailRequest()。带有源(“invitation@myserver.com");
List to address=new ArrayList();
ToAddresss.add(emailTo);
Destination dest=新目的地()。带有ToAddress(ToAddresss);
请求。设置目的地(dest);
Content subcontent=new Content().withData(“我的服务邀请电子邮件”);
Message msg=new Message().withSubject(subcontent);
String textVer=nameFrom+“已邀请您尝试我的服务。”;
字符串htmlVer=“”+nameFrom+”已邀请您尝试我的服务。

”; //包括文本和HTML格式的正文 Content textContent=新内容(),带有数据(textVer); Content htmlContent=new Content().withData(htmlVer); Body Body=new Body().withHtml(htmlContent).withText(textContent); msg.setBody(body); request.setMessage(msg); 试试{ ses.发送电子邮件(请求); }捕获(AmazonServiceException ase){ 手部异常(ase); }捕获(AmazonClientException ace){ handleExceptions(ace); } }
在此基础上,我成功地发送了电子邮件,其中包括基于代码生成的外部变量的人员姓名。我的问题是,我将如何处理更复杂的HTML电子邮件?我已经生成了一个具有更复杂布局的HTML文件,但仍然需要通过代码修改这些变量。该文件是一个HTML,因此我认为(不确定)我可以将其作为一个大文本字符串读取,然后将其添加到htmlVer字符串中。但我想知道是否有更简单的方法来读取HTML文件,只需更改一些变量,然后简单地将其添加到AmazonSES的内容部分


我在这里采取了错误的方法吗?

您可以使用像Thymeleaf这样的模板引擎来处理html和注入属性

就这么简单:

ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setTemplateMode("HTML5");
resolver.setSuffix(".html");
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(resolver);
final Context context = new Context(Locale.CANADA);
String name = "John Doe";
context.setVariable("name", name);

final String html = templateEngine.process("myhtml", context);
使用
myhtml.html
文件:

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-3.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>My first template with Thymeleaf</title>
</head>
<body>
    <p th:text="${name}">A Random Name</p> 
</body>
</html>

我的第一个使用Thymeleaf的模板

随机名称

引擎处理HTML文件后,java代码中的变量
HTML
将包含上面的HTML,但已将
元素的内容替换为您在上下文中传递的值


如果您使用像DreamWeaver这样的工具来制作html,这不是问题。您可以使用文本编辑器,稍后添加
th:text
(或其他)属性。这是Thymeleaf的优势之一,您可以单独完成模板和java代码,并在需要时加入它们。

使用html模板引擎/框架,如。下面是一个电子邮件示例:,使用spring,但显然可以使用基本的servlet。我发现了名为velocity的引擎框架。或者你刚才提到的那个。但不管怎样,我都必须在这些引擎中创建HTML,对吗?我无法使用dreamweaver之类的工具创建它?您可以使用dreamweaver创建它,获取生成的HTML,然后添加特定于thymeleaf或velocity的属性。非常感谢,我在计算最后一部分时遇到了问题。嘿,我想知道您是否碰巧知道我应该在哪里找到我的文件。在我的服务器结构中,因为我不断收到无法找到html文件的错误。我以为它在WEB-INF文件夹中,但找不到。在这种情况下,我相信它应该在类路径上。因此,如果您在某个IDE中,请将一个文件夹设为源文件夹并添加到其中。否则,在运行它时,使用-cp将其添加到类路径。