许鹏程
2023-06-30 3bbfaa3d7d416afbd154576453c8ee9e7e2f8899
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.product;
import com.alibaba.fastjson.JSON;
import com.sendgrid.Email;
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
public class SendGridAPI {
 
    private static Logger logger = LoggerFactory.getLogger(SendGridAPI.class);
    
    public static void main(String[] args) throws Exception {
        
      
    }
 
    /**
     * 组织邮件内容模板
     * @param from 发件方
     * @param to 收件方
     * @param templateId 模板ID
     * @param params 参数
     * @return
     */
    private static String getEmailContent(String from, String to, String templateId, Map<String, String> params){
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("from", new Email(from));
        dataMap.put("template_id", templateId);
        Map<String, Object> persMap = new HashMap<>();
        persMap.put("to", Arrays.asList(new Email(to)));
        persMap.put("dynamic_template_data", params);
        dataMap.put("personalizations", Arrays.asList(persMap));
        return JSON.toJSONString(dataMap);
    }
}