java SendGrid:模板和替换标记
我创建了一个sendgrid模板,以便能够根据用户信息生成电子邮件。到现在为止,这真的很简单:
<html>
<body>
<div><%body%></div>
<div>Hi there :username!</div>
<div>Please, click on here to complete Accoung Activation: :activation</div>
<div>Please, bear with us.</div>
</body>
</html>
据我所知,我能够替换代币(:username
和:activation
)
然而,我不太明白如何在java上构建它。到目前为止,我已经能够编写以下代码,以便发送带有模板的电子邮件:
String activationUri = "http://activation uri.sample.com/activation";
String address = "sample@sample.com";
Email from = new Email("no-reply@facetz.zone");
String subject = "Account activation mail request";
Email to = new Email(address);
Content content = new Content("text/plain", activationUri);
Mail mail = new Mail(from, subject, to, content);
mail.setTemplateId("7928c2b2-c5a9-4918-a035-db5b7aae532b");
SendGrid sg = new SendGrid("api_key");
Request request = new Request();
try {
request.method = Method.POST;
request.endpoint = "mail/send";
request.body = mail.build();
Response response = sg.api(request);
} catch (IOException ex) {
throw MailGenerationException.create(address, ex);
}
正如你所看到的,我已经设置了templateId
,然而,我不知道如何:
- 设置模板版本李>
- 添加代币替换李>
另一方面:
- {
}和{ }以及{ }和{ }标签之间的区别是什么李>
拜托,我已经看过文件了。到目前为止,我还不能理解我提出的所有问题
# 1 楼答案
我想做一些类似的事情,但我找不到一个方法来完成一个请求
将使用的模板始终是“活动”模板,因此,要选择不同的版本,必须首先调用templates/versions端点并“激活”它
假设您使用的是API版本3,那么您将执行以下操作(在实际发送电子邮件之前):
要检索模板版本列表,需要调用templates endpoint。。。然后,版本的使用就变得有点乏味了
对于替换,必须构建个性化对象:
Personalization类非常有用,因为它可以保存收件人(抄送、密件抄送和收件人)和其他数据
<;%身体%>标记将被您在邮件中发送的任何内容替换。身体,和<;%主题%>被个性化对象(或mail.subject)中设置的主题替换。与其他标签的唯一区别是,这些标签不需要通过Personalization对象进行设置
顺便说一下,主题可以包含其他标签,这些标签也将被替换
希望这对你有帮助