-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6400701
commit 2ccd54d
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import com.sendgrid.*; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
////////////////////////////////////////////////////////////////// | ||
// Set data residency to navigate to a region/edge. | ||
// Currently supported: "global", "eu" | ||
|
||
|
||
public class Example { | ||
public static void main(String[] args) throws IOException { | ||
try { | ||
|
||
final Mail helloWorld = buildHelloEmail(); | ||
|
||
Request request = new Request(); | ||
request.setEndpoint("mail/send"); | ||
request.setBody(helloWorld.build()); | ||
request.setMethod(Method.POST); | ||
|
||
// sending to global data residency | ||
Sendgrid sg = buildSendgridObj("global"); | ||
Response response = sg.api(request); | ||
System.out.println("Sending to hostname: " + sg.getHost()); | ||
System.out.println(response.getStatusCode()); | ||
System.out.println(response.getBody()); | ||
System.out.println(response.getHeaders()); | ||
|
||
// sending to EU data residency | ||
Sendgrid sg = buildSendgridObj("eu"); | ||
Response response = sg.api(request); | ||
System.out.println("Sending to hostname: " + sg.getHost()); | ||
System.out.println(response.getStatusCode()); | ||
System.out.println(response.getBody()); | ||
System.out.println(response.getHeaders()); | ||
|
||
// not configuring any region defaults to global | ||
Sendgrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY")); | ||
Response response = sg.api(request); | ||
System.out.println("Sending to hostname: " + sg.getHost()); | ||
System.out.println(response.getStatusCode()); | ||
System.out.println(response.getBody()); | ||
System.out.println(response.getHeaders()); | ||
|
||
} catch (IOException ex) { | ||
throw ex; | ||
} | ||
} | ||
|
||
public static Mail buildHelloEmail() { | ||
Email from = new Email("[email protected]"); | ||
String subject = "Hello World from the Twilio SendGrid Java Library"; | ||
Email to = new Email("[email protected]"); | ||
Content content = new Content("text/plain", "some text here"); | ||
// Note that when you use this constructor an initial personalization object | ||
// is created for you. It can be accessed via | ||
// mail.personalization.get(0) as it is a List object | ||
Mail mail = new Mail(from, subject, to, content); | ||
Email email = new Email("[email protected]"); | ||
mail.personalization.get(0).addTo(email); | ||
|
||
return mail; | ||
} | ||
|
||
public static Sendgrid buildSendgridObj(String region){ | ||
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY")); | ||
sg.setDataResidency(region); | ||
return sg; | ||
|
||
} | ||
} | ||
|