Skip to content

Commit 4deb2f4

Browse files
2 parents c23507d + bd65fc7 commit 4deb2f4

23 files changed

+717
-363
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Add the following to your build.gradle file in the root of your project.
6767
...
6868
dependencies {
6969
...
70-
compile 'com.sendgrid:sendgrid-java:4.2.1'
70+
implementation 'com.sendgrid:sendgrid-java:4.2.1'
7171
}
7272
7373
repositories {
@@ -219,8 +219,8 @@ Quick links:
219219

220220
- [Feature Request](https://github.com/sendgrid/sendgrid-java/blob/master/CONTRIBUTING.md#feature-request)
221221
- [Bug Reports](https://github.com/sendgrid/sendgrid-java/blob/master/CONTRIBUTING.md#submit-a-bug-report)
222-
- [Sign the CLA to Create a Pull Request](https://github.com/sendgrid/sendgrid-java/blob/master/CONTRIBUTING.md#cla)
223222
- [Improvements to the Codebase](https://github.com/sendgrid/sendgrid-java/blob/master/CONTRIBUTING.md#improvements-to-the-codebase)
223+
- [Sign the CLA to Create a Pull Request](https://cla.sendgrid.com/sendgrid/sendgrid-java)
224224

225225
<a name="troubleshooting"></a>
226226
# Troubleshooting
@@ -230,7 +230,9 @@ Please see our [troubleshooting guide](https://github.com/sendgrid/sendgrid-java
230230
<a name="about"></a>
231231
# About
232232

233-
sendgrid-java is guided and supported by the SendGrid [Developer Experience Team](mailto:[email protected]).
233+
sendgrid-java is guided and supported by the SendGrid Developer Experience Team.
234+
235+
Please email the Developer Experience Team [here](mailto:[email protected]) in case of any queries.
234236

235237
sendgrid-java is maintained and funded by SendGrid, Inc. The names and logos for sendgrid-java are trademarks of SendGrid, Inc.
236238

USE_CASES.md

Lines changed: 129 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ This documentation provides examples for specific use cases. Please [open an iss
22

33
# Table of Contents
44

5-
* [Transactional Templates](#transactional_templates)
6-
* [How to Setup a Domain Whitelabel](#domain_whitelabel)
7-
* [How to View Email Statistics](#email_stats)
5+
* [Transactional Templates](#transactional-templates)
6+
* [Legacy Templates](#legacy-templates)
7+
* [How to Setup a Domain Whitelabel](#domain-whitelabel)
8+
* [How to View Email Statistics](#email-stats)
89

910
<a name="transactional-templates"></a>
1011
# (LEGACY) Transactional Templates
@@ -15,6 +16,102 @@ For this example, we assume you have created a [transactional template](https://
1516

1617
Template ID (replace with your own):
1718

19+
```text
20+
d-2c214ac919e84170b21855cc129b4a5f
21+
```
22+
23+
Template Body:
24+
25+
```html
26+
<html>
27+
<head>
28+
<title></title>
29+
</head>
30+
<body>
31+
Hello {{name}},
32+
<br/><br/>
33+
I'm glad you are trying out the template feature!
34+
<br/><br/>
35+
I hope you are having a great day in {{city}} :)
36+
<br/><br/>
37+
</body>
38+
</html>
39+
```
40+
41+
## With Mail Helper Class
42+
43+
```java
44+
import com.sendgrid.*;
45+
import java.io.IOException;
46+
47+
public class Example {
48+
public static void main(String[] args) throws IOException {
49+
Mail mail = new Mail();
50+
mail.setFrom(new Email("[email protected]"));
51+
mail.setTemplateId("d-2c214ac919e84170b21855cc129b4a5f");
52+
53+
Personalization personalization = new Personalization();
54+
personalization.addDynamicTemplateData("name", "Example User");
55+
personalization.addDynamicTemplateData("city", "Denver");
56+
personalization.addTo(new Email("[email protected]"));
57+
mail.addPersonalization(personalization);
58+
59+
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
60+
Request request = new Request();
61+
try {
62+
request.setMethod(Method.POST);
63+
request.setEndpoint("mail/send");
64+
request.setBody(mail.build());
65+
Response response = sg.api(request);
66+
System.out.println(response.getStatusCode());
67+
System.out.println(response.getBody());
68+
System.out.println(response.getHeaders());
69+
} catch (IOException ex) {
70+
throw ex;
71+
}
72+
}
73+
}
74+
```
75+
76+
## Without Mail Helper Class
77+
78+
```java
79+
import com.sendgrid.*;
80+
import java.io.IOException;
81+
82+
public class Example {
83+
public static void main(String[] args) throws IOException {
84+
try {
85+
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
86+
Request request = new Request();
87+
request.setMethod(Method.POST);
88+
request.setEndpoint("mail/send");
89+
request.setBody("{
90+
\"from\": {\"email\": \"[email protected]\"},
91+
\"personalizations\":
92+
[{
93+
\"to\": [{\"email\": \"[email protected]\"}],
94+
\"dynamic_template_data\": {\"name\": \"Example User\", \"city\": \"Denver\"}
95+
}],
96+
\"template_id\": \"d-2c214ac919e84170b21855cc129b4a5f\"}");
97+
Response response = sg.api(request);
98+
System.out.println(response.getStatusCode());
99+
System.out.println(response.getBody());
100+
System.out.println(response.getHeaders());
101+
} catch (IOException ex) {
102+
throw ex;
103+
}
104+
}
105+
}
106+
```
107+
108+
<a name="legacy-templates"></a>
109+
# Legacy Templates
110+
111+
For this example, we assume you have created a [legacy template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing.
112+
113+
Template ID (replace with your own):
114+
18115
```text
19116
13b8f94f-bcae-4ec6-b752-70d6cb59f932
20117
```
@@ -29,19 +126,19 @@ Template Body:
29126

30127
```html
31128
<html>
32-
<head>
33-
<title></title>
34-
</head>
35-
<body>
36-
Hello -name-,
37-
<br /><br/>
38-
I'm glad you are trying out the template feature!
39-
<br /><br/>
40-
<%body%>
41-
<br /><br/>
42-
I hope you are having a great day in -city- :)
43-
<br /><br/>
44-
</body>
129+
<head>
130+
<title></title>
131+
</head>
132+
<body>
133+
Hello -name-,
134+
<br /><br/>
135+
I'm glad you are trying out the template feature!
136+
<br /><br/>
137+
<%body%>
138+
<br /><br/>
139+
I hope you are having a great day in -city- :)
140+
<br /><br/>
141+
</body>
45142
</html>
46143
```
47144

@@ -92,7 +189,20 @@ public class Example {
92189
Request request = new Request();
93190
request.setMethod(Method.POST);
94191
request.setEndpoint("mail/send");
95-
request.setBody("{\"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\"}");
192+
request.setBody("{
193+
\"personalizations\":
194+
[{
195+
\"to\": [{\"email\": \"[email protected]\"}],
196+
\"substitutions\": {\"-name-\": \"Example User\", \"-city-\": \"Denver\"},
197+
\"subject\": \"Hello World from the SendGrid Java Library!\"
198+
}],
199+
\"from\": {\"email\": \"[email protected]\"},
200+
\"content\":
201+
[{
202+
\"type\": \"text/html\",
203+
\"value\": \"I'm replacing the <strong>body tag</strong>\"
204+
}]
205+
,\"template_id\": \"13b8f94f-bcae-4ec6-b752-70d6cb59f932\"}");
96206
Response response = sg.api(request);
97207
System.out.println(response.getStatusCode());
98208
System.out.println(response.getBody());
@@ -104,14 +214,14 @@ public class Example {
104214
}
105215
```
106216

107-
<a name="domain_whitelabel"></a>
217+
<a name="domain-whitelabel"></a>
108218
# How to Setup a Domain Whitelabel
109219

110220
You can find documentation for how to setup a domain whitelabel via the UI [here](https://sendgrid.com/docs/Classroom/Basics/Whitelabel/setup_domain_whitelabel.html) and via API [here](https://github.com/sendgrid/sendgrid-java/blob/master/USAGE.md#whitelabel).
111221

112222
Find more information about all of SendGrid's whitelabeling related documentation [here](https://sendgrid.com/docs/Classroom/Basics/Whitelabel/index.html).
113223

114-
<a name="email_stats"></a>
224+
<a name="email-stats"></a>
115225
# How to View Email Statistics
116226

117227
You can find documentation for how to view your email statistics via the UI [here](https://app.sendgrid.com/statistics) and via API [here](https://github.com/sendgrid/sendgrid-java/blob/master/USAGE.md#stats).

docker/USAGE.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ You can mount repositories in the `/mnt/sendgrid-java` and `/mnt/java-http-clien
2323

2424
<a name="Testing"></a>
2525
# Testing
26-
Testing is easy! Run the container, `cd sendgrid`, and run `./gradlew test`.
26+
Testing is easy!
27+
1. Run the container: `docker run -it sendgrid/sendgrid-java`
28+
2. `cd sendgrid-java`
29+
3. run `./gradlew test`
2730

2831
<a name="about"></a>
2932
# About

examples/accesssettings/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
![SendGrid Logo](https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)
2-
+
3-
+This folder contains various examples on using the ACCESS_SETTINGS endpoint of SendGrid with Java:
4-
+
5-
+* [Retrieve a list of currently whitelisted IPs (GET /access_settings/whitelist)](GetAccessSettings.java)
6-
+* [Retrieve a specific whitelisted IP (GET /access_settings/whitelist/{rule_id})](GetIPFromAccessSettings.java)
7-
+* [Retrieve a list of currently whitelisted IPs (GET /access_settings/whitelist)](GetAccessSettingsActivity.java)
8-
+* [Remove a specific IP from the whitelist (DELETE /access_settings/whitelist/{rule_id}](DeleteIPFromAccessSettings.java)
9-
+* [Remove one or more IPs from the whitelist (DELETE /access_settings/whitelist)](DeleteAccessSettings.java)
10-
+* [Add one or more IPs to the whitelist (POST /access_settings/whitelist)](CreateAccessSettings.java)
2+
3+
This folder contains various examples on using the ACCESS_SETTINGS endpoint of SendGrid with Java:
4+
5+
* [Retrieve a list of currently whitelisted IPs (GET /access_settings/whitelist)](GetAccessSettings.java)
6+
* [Retrieve a specific whitelisted IP (GET /access_settings/whitelist/{rule_id})](GetIPFromAccessSettings.java)
7+
* [Retrieve a list of currently whitelisted IPs (GET /access_settings/whitelist)](GetAccessSettingsActivity.java)
8+
* [Remove a specific IP from the whitelist (DELETE /access_settings/whitelist/{rule_id}](DeleteIPFromAccessSettings.java)
9+
* [Remove one or more IPs from the whitelist (DELETE /access_settings/whitelist)](DeleteAccessSettings.java)
10+
* [Add one or more IPs to the whitelist (POST /access_settings/whitelist)](CreateAccessSettings.java)

examples/helpers/mail/Example.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,26 @@ public static Mail buildKitchenSink() throws IOException {
207207
return mail;
208208
}
209209

210+
// API V3 Dynamic Template implementation
211+
public static Mail buildDynamicTemplate() throws IOException {
212+
Mail mail = new Mail();
213+
214+
Email fromEmail = new Email();
215+
fromEmail.setName("Example User");
216+
fromEmail.setEmail("[email protected]");
217+
mail.setFrom(fromEmail);
218+
219+
mail.setTemplateId("d-c6dcf1f72bdd4beeb15a9aa6c72fcd2c");
220+
221+
Personalization personalization = new Personalization();
222+
personalization.addDynamicTemplateData("name", "Example User");
223+
personalization.addDynamicTemplateData("city", "Denver");
224+
personalization.addTo(new Email("[email protected]"));
225+
mail.addPersonalization(personalization);
226+
227+
return mail;
228+
}
229+
210230
// Minimum required to send an email
211231
public static Mail buildHelloEmail() throws IOException {
212232
Email from = new Email("[email protected]");
@@ -261,8 +281,28 @@ public static void kitchenSinkExample() throws IOException {
261281
}
262282
}
263283

284+
public static void dynamicTemplateExample() throws IOException {
285+
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
286+
sg.addRequestHeader("X-Mock", "true");
287+
288+
Request request = new Request();
289+
Mail dynamicTemplate = buildDynamicTemplate();
290+
try {
291+
request.setMethod(Method.POST);
292+
request.setEndpoint("mail/send");
293+
request.setBody(dynamicTemplate.build());
294+
Response response = sg.api(request);
295+
System.out.println(response.getStatusCode());
296+
System.out.println(response.getBody());
297+
System.out.println(response.getHeaders());
298+
} catch (IOException ex) {
299+
throw ex;
300+
}
301+
}
302+
264303
public static void main(String[] args) throws IOException {
265-
baselineExample();
266-
kitchenSinkExample();
304+
// baselineExample();
305+
// kitchenSinkExample();
306+
dynamicTemplateExample();
267307
}
268308
}

examples/ips/AddToPool.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
//////////////////////////////////////////////////////////////////
12+
// Add an IP address to a pool
13+
// POST /ips/pools/{pool_name}/ips
14+
15+
16+
public class AddIPToPool {
17+
public static void main(String[] args) throws IOException {
18+
try {
19+
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
20+
Request request = new Request();
21+
request.setMethod(Method.POST);
22+
request.setEndpoint("ips/pools/{pool_name}/ips");
23+
request.setBody("{\"ip\":\"0.0.0.0\"}");
24+
Response response = sg.api(request);
25+
System.out.println(response.getStatusCode());
26+
System.out.println(response.getBody());
27+
System.out.println(response.getHeaders());
28+
} catch (IOException ex) {
29+
throw ex;
30+
}
31+
}
32+
}

examples/ips/AddToWarmup.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
12+
//////////////////////////////////////////////////////////////////
13+
// Add an IP to warmup
14+
// POST /ips/warmup
15+
16+
17+
public class AddIPToWarmup {
18+
public static void main(String[] args) throws IOException {
19+
try {
20+
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
21+
Request request = new Request();
22+
request.setMethod(Method.POST);
23+
request.setEndpoint("ips/warmup");
24+
request.setBody("{\"ip\":\"0.0.0.0\"}");
25+
Response response = sg.api(request);
26+
System.out.println(response.getStatusCode());
27+
System.out.println(response.getBody());
28+
System.out.println(response.getHeaders());
29+
} catch (IOException ex) {
30+
throw ex;
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)