SendGrid模板使用方式二
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/mao2080/
1、问题描述
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跑的时候报这个错误,具体代码如下:
2、解决方法
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、效果
3、其他方式
https://www.cnblogs.com/mao2080/p/12655793.html
4、参考网站
https://github.com/sendgrid/sendgrid-java/blob/master/USE_CASES.md
个性签名:1.01的365次方=37.78343433289 >>>1
0.99的365次方= 0.02551796445229 <<<1
每天进步一点点的目标,贵在坚持…