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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM