|
| 1 | +import com.fasterxml.jackson.databind.JsonNode; |
| 2 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 3 | + |
| 4 | +import com.sendgrid.*; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +////////////////////////////////////////////////////////////////// |
| 11 | +// Set data residency to navigate to a region/edge. |
| 12 | +// Currently supported: "global", "eu" |
| 13 | + |
| 14 | + |
| 15 | +public class Example { |
| 16 | + public static void main(String[] args) throws IOException { |
| 17 | + try { |
| 18 | + |
| 19 | + final Mail helloWorld = buildHelloEmail(); |
| 20 | + |
| 21 | + Request request = new Request(); |
| 22 | + request.setEndpoint("mail/send"); |
| 23 | + request.setBody(helloWorld.build()); |
| 24 | + request.setMethod(Method.POST); |
| 25 | + |
| 26 | + // sending to global data residency |
| 27 | + Sendgrid sg = buildSendgridObj("global"); |
| 28 | + Response response = sg.api(request); |
| 29 | + System.out.println("Sending to hostname: " + sg.getHost()); |
| 30 | + System.out.println(response.getStatusCode()); |
| 31 | + System.out.println(response.getBody()); |
| 32 | + System.out.println(response.getHeaders()); |
| 33 | + |
| 34 | + // sending to EU data residency |
| 35 | + Sendgrid sg = buildSendgridObj("eu"); |
| 36 | + Response response = sg.api(request); |
| 37 | + System.out.println("Sending to hostname: " + sg.getHost()); |
| 38 | + System.out.println(response.getStatusCode()); |
| 39 | + System.out.println(response.getBody()); |
| 40 | + System.out.println(response.getHeaders()); |
| 41 | + |
| 42 | + // not configuring any region defaults to global |
| 43 | + Sendgrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY")); |
| 44 | + Response response = sg.api(request); |
| 45 | + System.out.println("Sending to hostname: " + sg.getHost()); |
| 46 | + System.out.println(response.getStatusCode()); |
| 47 | + System.out.println(response.getBody()); |
| 48 | + System.out.println(response.getHeaders()); |
| 49 | + |
| 50 | + } catch (IOException ex) { |
| 51 | + throw ex; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public static Mail buildHelloEmail() { |
| 56 | + Email from = new Email("test@example.com"); |
| 57 | + String subject = "Hello World from the Twilio SendGrid Java Library"; |
| 58 | + Email to = new Email("test@example.com"); |
| 59 | + Content content = new Content("text/plain", "some text here"); |
| 60 | + // Note that when you use this constructor an initial personalization object |
| 61 | + // is created for you. It can be accessed via |
| 62 | + // mail.personalization.get(0) as it is a List object |
| 63 | + Mail mail = new Mail(from, subject, to, content); |
| 64 | + Email email = new Email("test2@example.com"); |
| 65 | + mail.personalization.get(0).addTo(email); |
| 66 | + |
| 67 | + return mail; |
| 68 | + } |
| 69 | + |
| 70 | + public static Sendgrid buildSendgridObj(String region){ |
| 71 | + SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY")); |
| 72 | + sg.setDataResidency(region); |
| 73 | + return sg; |
| 74 | + |
| 75 | + } |
| 76 | +} |
| 77 | + |
0 commit comments