0

我已经构建了一个小型 Java 桌面应用程序,它也可以发送电子邮件。一切正常,但我想为电子邮件使用 html 模板。有谁我该怎么做?我已经做好了:

HtmlEmail email = new HtmlEmail();
email.setHtmlMsg(htmlString);

其中 htmlString 是这样的:

String htmlString= "<html><table><tr><td width='200px'>Name</td><td width='200px'>Start Date</td><td width='200px'>Deadline</td>"
4

1 回答 1

1

您可以将模板放在Resource Bundle文件中。通常,这类文件有一个.properties扩展名,其内容的每一行都遵循模式key=value。这些文件必须在classpath.

例如,如果你有一个资源包文件,命名resources.properties并放置在一个包中,命名somepackage并具有以下内容:

template.email=<html><table><tr><td width='200px'>{1}</td><td width='200px'>{2}</td><td width='200px'>{3}</td>

请注意,使用{1},{2}并且{3}我已经标记了那些必须替换的消息片段,以便在需要时构建完整的消息。

为了从资源包中获取所有消息,您需要执行以下操作:

ResourceBundle rb = ResourceBundle.getBundle("somepackage.resources");
Enumeration <String> keys = rb.getKeys();
while (keys.hasMoreElements()) {
    String key = keys.nextElement();
    String value = rb.getString(key);
    System.out.println(key + ": " + value);
}

你可以在这里找到更多信息。

于 2013-08-01T08:27:19.037 回答