创建了 sendgrid 电子邮件模板,如何从 Java 调用它

pja*_*son 8 java sendgrid

我在 sendgrid 中创建了电子邮件模板 - 具有可替换的值;

我从rabbitMQ队列获取用于处理电子邮件的JSON负载(包含替代值)。我的问题是如何从 Java 调用 sendgrid 电子邮件模板?

pja*_*son 4

我找到了解决方案,通过sendgrid调用sendgrid模板和电子邮件的方法如下:

SendGrid sendGrid = new SendGrid("username","password"));
SendGrid.Email email = new SendGrid.Email();

//Fill the required fields of email
email.setTo(new String[] { "xyz_to@gmail.com"});
email.setFrom("xyz_from@gmail.com");
email.setSubject(" ");
email.setText(" ");
email.setHtml(" ");

// Substitute template ID
email.addFilter(
    "templates",
    "template_id",
    "1231_1212_2323_3232");

//place holders in template, dynamically fill values in template
email.addSubstitution(
     ":firstName",
     new String[] { firstName });
email.addSubstitution(
     ":lastName",
     new String[] { lastName });

// send your email
Response response = sendGrid.send(email);
Run Code Online (Sandbox Code Playgroud)