Exception in thread "main" java.io.IOException: Request returned status Code 400Body:{"errors":[{"message":"Substitutions may not be used with dynamic templating","field":"personalizations.0.substitutions","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.substitutions"}]}。使用SendGrid GitHub demo跑的时候报这个错误,具体代码如下:

1、相关Pom
<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<version>4.0.1</version>
</dependency>
2、相关Code,变量使用-- 连接起来
package com.sendgrid;
public class Example4 {
public static void main(String[] args) throws Exception {
Email from = new Email("xxxx@qq.com");
String subject = "I‘m replacing the subject tag";
Email to = new Email("mao2080@sina.com");
Content content = new Content("text/html", "<html>hi -name-, today is -date-, phone:-phone-</html>");
Mail mail = new Mail(from, subject, to, content);
mail.personalization.get(0).addSubstitution("-name-", "Jack Chen");
mail.personalization.get(0).addSubstitution("-date-", "2020-04-07");
mail.personalization.get(0).addSubstitution("-phone-", "0755-8888xxxx");
String apiKey = "SG.YYY.XXX";
SendGrid sg = new SendGrid(apiKey);
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 (Exception ex) {
throw ex;
}
}
}
3、效果

https://www.cnblogs.com/mao2080/p/12655793.html
https://github.com/sendgrid/sendgrid-java/blob/master/USE_CASES.md
原文:https://www.cnblogs.com/mao2080/p/12655939.html