【问题标题】:Sendgrid v3: "Substitutions may not be used with dynamic templating"Sendgrid v3:“替换不能与动态模板一起使用”
【发布时间】:2018-11-02 00:22:14
【问题描述】:

我正在尝试将我的 API 代码从 Sendgrid v2 更新到实际的 Sendgrid v3,所以我的代码过去看起来像这样:

public void sendCreatedUserEmail(User user) {
    Email from = new Email(FROM);
    from.setName(EMAIL_NAME);
    String subject = "Hello" + user.getName();
    Email to = new Email(user.getEmail());
    Content content = new Content("text/html", "Something");
    Mail mail = new Mail(from, subject, to, content);
    mail.personalization.get(0).addSubstitution("{name1}", user.getName());
    mail.personalization.get(0).addSubstitution("{name2}", user.getName());
    mail.setTemplateId(USER_TEMPLATE_ID);
    SendGrid sg = new SendGrid(SENDGRID_API_KEY);
    Request request = new Request();
    try {
        request.setMethod(Method.POST);
        request.setEndpoint("mail/send");
        request.setBody(mail.build());
        Response response = sg.api(request);
    } catch (IOException ex) {
        logger.error(ex);
    }
}

经过几个小时的研究,我将 v3 更改为: (为了更清晰的视图,我把所有东西都分开了)

public void sendCreatedUserEmail(User user) {
    Mail mail = new Mail();

    Email from = new Email();
    from.setName(EMAIL_NAME);
    from.setEmail(FROM);
    mail.setFrom(from);

    String subject = "Hello, " + user.getName();
    mail.setSubject(subject);

    Personalization personalization = new Personalization();

    Email to = new Email();
    to.setEmail(user.getEmail());
    to.setName(user.getName());
    personalization.addTo(to);

    personalization.setSubject(subject);

    personalization.addSubstitution("{name2}",user.getName());
    personalization.addSubstitution("{name1}",user.getName());

    mail.addPersonalization(personalization);

    Content content = new Content();
    content.setType("text/html");
    content.setValue("Something");
    mail.addContent(content);

    mail.setTemplateId(NEW_USER_TEMPLATE_ID);

    SendGrid sg = new SendGrid(SENDGRID_API_KEY);

    Request request = new Request();
    try {
        request.setMethod(Method.POST);
        request.setEndpoint("mail/send");
        request.setBody(mail.build());
        Response response = sg.api(request);
        System.out.println(response.getStatusCode());
        System.out.println(response.getBody());
        System.out.println(response.getHeaders());
    } catch (IOException ex) {
        logger.error(ex);
    }
}

我收到以下错误:

ERROR ROOT - java.io.IOException: Request returned status Code 400Body:{"errors":[{"message":"Substitutions may not be used with dynamic template","field":"personalizations.0.substitutions ","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.substitutions"}]}

我真的不知道该怎么做!我一直在阅读 sendgrid 文档,但我无法得到它。

一些可能有帮助的细节 - Java8 是语言 - 依赖关系的 MAVEN - 用于 IDE 的 IntelliJ

抱歉可能出现的错误,这是我的第一篇文章,英语不是我的主要语言。谢谢!

【问题讨论】:

    标签: java maven sendgrid sendgrid-api-v3 sendgrid-templates


    【解决方案1】:

    Sendgrid API V3 使用动态模板数据而不是替换。

    试试这个而不是使用addSubstitution

    personalization.addDynamicTemplateData("{name2}",user.getName());
    personalization.addDynamicTemplateData("{name1}",user.getName());
    

    来源:

    https://github.com/sendgrid/sendgrid-java/blob/9bc569cbdb908dba609ed0d9d2691dff319ce155/src/main/java/com/sendgrid/helpers/mail/objects/Personalization.java

    https://sendgrid.com/docs/ui/sending-email/how-to-send-an-email-with-dynamic-transactional-templates/

    【讨论】:

    • 正是我所需要的。谢谢!
    【解决方案2】:

    试试:

    personalization.addDynamicTemplateData("name2",user.getName());
    personalization.addDynamicTemplateData("name1",user.getName());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多