|
| 1 | +This documentation provides examples for specific use cases. Please [open an issue](https://github.com/sendgrid/sendgrid-java/issues) or make a pull request for any use cases you would like us to document here. Thank you! |
| 2 | + |
| 3 | +# Table of Contents |
| 4 | + |
| 5 | +* [Transactional Templates](#transactional_templates) |
| 6 | + |
| 7 | +<a name="transactional_templates"></a> |
| 8 | +# Transactional Templates |
| 9 | + |
| 10 | +For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing. |
| 11 | + |
| 12 | +Template ID (replace with your own): |
| 13 | + |
| 14 | +```text |
| 15 | +13b8f94f-bcae-4ec6-b752-70d6cb59f932 |
| 16 | +``` |
| 17 | + |
| 18 | +Email Subject: |
| 19 | + |
| 20 | +```text |
| 21 | +<%subject%> |
| 22 | +``` |
| 23 | + |
| 24 | +Template Body: |
| 25 | + |
| 26 | +```html |
| 27 | +<html> |
| 28 | +<head> |
| 29 | + <title></title> |
| 30 | +</head> |
| 31 | +<body> |
| 32 | +Hello -name-, |
| 33 | +<br /><br/> |
| 34 | +I'm glad you are trying out the template feature! |
| 35 | +<br /><br/> |
| 36 | +<%body%> |
| 37 | +<br /><br/> |
| 38 | +I hope you are having a great day in -city- :) |
| 39 | +<br /><br/> |
| 40 | +</body> |
| 41 | +</html> |
| 42 | +``` |
| 43 | + |
| 44 | +## With Mail Helper Class |
| 45 | + |
| 46 | +```java |
| 47 | +import com.sendgrid.*; |
| 48 | +import java.io.IOException; |
| 49 | + |
| 50 | +public class Example { |
| 51 | + public static void main(String[] args) throws IOException { |
| 52 | + Email from = new Email( "[email protected]"); |
| 53 | + String subject = "I'm replacing the subject tag"; |
| 54 | + Email to = new Email( "[email protected]"); |
| 55 | + Content content = new Content("text/html", "I'm replacing the <strong>body tag</strong>"); |
| 56 | + Mail mail = new Mail(from, subject, to, content); |
| 57 | + mail.personalization.get(0).addSubstitution("-name-", "Example User"); |
| 58 | + mail.personalization.get(0).addSubstitution("-city-", "Denver"); |
| 59 | + mail.setTemplateId("13b8f94f-bcae-4ec6-b752-70d6cb59f932"); |
| 60 | + |
| 61 | + SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY")); |
| 62 | + Request request = new Request(); |
| 63 | + try { |
| 64 | + request.method = Method.POST; |
| 65 | + request.endpoint = "mail/send"; |
| 66 | + request.body = mail.build(); |
| 67 | + Response response = sg.api(request); |
| 68 | + System.out.println(response.statusCode); |
| 69 | + System.out.println(response.body); |
| 70 | + System.out.println(response.headers); |
| 71 | + } catch (IOException ex) { |
| 72 | + throw ex; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +## Without Mail Helper Class |
| 79 | + |
| 80 | +```java |
| 81 | +import com.sendgrid.*; |
| 82 | +import java.io.IOException; |
| 83 | + |
| 84 | +public class Example { |
| 85 | + public static void main(String[] args) throws IOException { |
| 86 | + try { |
| 87 | + SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY")); |
| 88 | + Request request = new Request(); |
| 89 | + request.method = Method.POST; |
| 90 | + request.endpoint = "mail/send"; |
| 91 | + request .body = "{\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}],\"substitutions\":{\"-name-\":\"Example User\",\"-city-\":\"Denver\"},\"subject\":\"Hello World from the SendGrid Java Library!\"}],\"from\":{\"email\":\"[email protected]\"},\"content\":[{\"type\":\"text/html\",\"value\": \"I'm replacing the <strong>body tag</strong>\"}],\"template_id\": \"13b8f94f-bcae-4ec6-b752-70d6cb59f932\"}"; |
| 92 | + Response response = sg.api(request); |
| 93 | + System.out.println(response.statusCode); |
| 94 | + System.out.println(response.body); |
| 95 | + System.out.println(response.headers); |
| 96 | + } catch (IOException ex) { |
| 97 | + throw ex; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
0 commit comments