Skip to content

Commit b32e311

Browse files
committed
Added CC and BCC samples, added a small how-to-run-samples README section
1 parent 3287ac9 commit b32e311

File tree

5 files changed

+248
-7
lines changed

5 files changed

+248
-7
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,23 @@ private void sendEmail(String from, String[] recipients, String email) throws Sp
9696
}
9797

9898
```
99+
100+
## Running The Sample Apps
101+
102+
The sample apps are held in `apps/sparkpost-samples-app` with each sample's source code in `apps/sparkpost-samples-app/src/main/java/com/sparkpost/samples/`.
103+
104+
To build the samples:
105+
106+
```bash
107+
cd apps/sparkpost-samples-app
108+
mvn compile
109+
```
110+
111+
One the samples are built, create `config.properties` by copying `apps/sparkpost-samples-app/config.properties.example` and filling in your SparkPost API key and other test parameters.
112+
113+
You can now run your chosen sample through maven:
114+
115+
```bash
116+
mvn exec:java -Dexec.mainClass=com.sparkpost.samples.SendEmailCCSample
117+
```
118+

apps/sparkpost-samples-app/config.properties.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,7 @@ SPARKPOST_FROM=from_address@example.com
4646
##
4747
SPARKPOST_RECIPIENTS=recipient_one@example.com, recipient_two@example.com
4848

49+
SPARKPOST_CC_RECIPIENTS=cc_recipient_one@example.com, cc_recipient_two@example.com
50+
51+
SPARKPOST_BCC_RECIPIENTS=bcc_recipient_one@example.com, bcc_recipient_two@example.com
4952

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
package com.sparkpost.samples;
3+
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import org.apache.log4j.Level;
11+
import org.apache.log4j.Logger;
12+
13+
import org.apache.commons.lang3.StringUtils;
14+
15+
import com.sparkpost.Client;
16+
import com.sparkpost.exception.SparkPostException;
17+
import com.sparkpost.model.AddressAttributes;
18+
import com.sparkpost.model.RecipientAttributes;
19+
import com.sparkpost.model.TemplateContentAttributes;
20+
import com.sparkpost.model.TransmissionWithRecipientArray;
21+
import com.sparkpost.model.responses.Response;
22+
import com.sparkpost.resources.ResourceTransmissions;
23+
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
24+
import com.sparkpost.transport.RestConnection;
25+
26+
public class SendEmailBCCSample extends SparkPostBaseApp {
27+
28+
static final Logger logger = Logger.getLogger(CreateTemplateSimple.class);
29+
30+
private Client client;
31+
32+
public static void main(String[] args) throws SparkPostException, IOException {
33+
Logger.getRootLogger().setLevel(Level.DEBUG);
34+
35+
SendEmailBCCSample sample = new SendEmailBCCSample();
36+
sample.runApp();
37+
}
38+
39+
private void runApp() throws SparkPostException, IOException {
40+
this.client = this.newConfiguredClient();
41+
42+
// Loads an email to send from the file system
43+
String fromAddress = getFromAddress();
44+
String[] toRecipients = getTestRecipients();
45+
String[] bccRecipients = getBCCRecipients();
46+
47+
sendEmail(fromAddress, toRecipients, bccRecipients);
48+
}
49+
50+
private void sendEmail(String from, String[] toRecipients, String[] bccRecipients) throws SparkPostException {
51+
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();
52+
53+
// Populate Recipients
54+
List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();
55+
56+
// Primary 'To' recipients
57+
for (String to : toRecipients) {
58+
RecipientAttributes recipientAttribs = new RecipientAttributes();
59+
AddressAttributes addressAttribs = new AddressAttributes(to);
60+
recipientAttribs.setAddress(addressAttribs);
61+
recipientArray.add(recipientAttribs);
62+
}
63+
64+
// Secondary 'BCC' recipients with the primary recipients listed in 'To:' header
65+
String toHeader = stringArrayToCSV(toRecipients);
66+
for (String bcc : bccRecipients) {
67+
RecipientAttributes recipientAttribs = new RecipientAttributes();
68+
AddressAttributes addressAttribs = new AddressAttributes(bcc);
69+
addressAttribs.setHeaderTo(toHeader);
70+
recipientAttribs.setAddress(addressAttribs);
71+
recipientArray.add(recipientAttribs);
72+
}
73+
74+
transmission.setRecipientArray(recipientArray);
75+
76+
// Populate Email Body
77+
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
78+
contentAttributes.setFrom(new AddressAttributes(from));
79+
contentAttributes.setSubject("BCC Example");
80+
contentAttributes.setText("This message was sent To 1 recipient and some other recipients were quietly BCCd");
81+
contentAttributes.setHtml("<p>This message was sent To 1 recipient and some other recipients were quietly BCCd</p>");
82+
83+
transmission.setContentAttributes(contentAttributes);
84+
85+
// Send the Email
86+
RestConnection connection = new RestConnection(this.client, getEndPoint());
87+
Response response = ResourceTransmissions.create(connection, 0, transmission);
88+
89+
logger.debug("Transmission Response: " + response);
90+
}
91+
92+
}
93+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
package com.sparkpost.samples;
3+
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import org.apache.log4j.Level;
11+
import org.apache.log4j.Logger;
12+
13+
import org.apache.commons.lang3.StringUtils;
14+
15+
import com.sparkpost.Client;
16+
import com.sparkpost.exception.SparkPostException;
17+
import com.sparkpost.model.AddressAttributes;
18+
import com.sparkpost.model.RecipientAttributes;
19+
import com.sparkpost.model.TemplateContentAttributes;
20+
import com.sparkpost.model.TransmissionWithRecipientArray;
21+
import com.sparkpost.model.responses.Response;
22+
import com.sparkpost.resources.ResourceTransmissions;
23+
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
24+
import com.sparkpost.transport.RestConnection;
25+
26+
public class SendEmailCCSample extends SparkPostBaseApp {
27+
28+
static final Logger logger = Logger.getLogger(CreateTemplateSimple.class);
29+
30+
private Client client;
31+
32+
public static void main(String[] args) throws SparkPostException, IOException {
33+
Logger.getRootLogger().setLevel(Level.DEBUG);
34+
35+
SendEmailCCSample sample = new SendEmailCCSample();
36+
sample.runApp();
37+
}
38+
39+
private void runApp() throws SparkPostException, IOException {
40+
this.client = this.newConfiguredClient();
41+
42+
// Loads an email to send from the file system
43+
String fromAddress = getFromAddress();
44+
String[] toRecipients = getTestRecipients();
45+
String[] ccRecipients = getCCRecipients();
46+
47+
sendEmail(fromAddress, toRecipients, ccRecipients);
48+
}
49+
50+
private void sendEmail(String from, String[] toRecipients, String[] ccRecipients) throws SparkPostException {
51+
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();
52+
53+
// Populate Recipients
54+
List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();
55+
56+
// Primary 'To' recipients
57+
for (String to : toRecipients) {
58+
RecipientAttributes recipientAttribs = new RecipientAttributes();
59+
AddressAttributes addressAttribs = new AddressAttributes(to);
60+
recipientAttribs.setAddress(addressAttribs);
61+
recipientArray.add(recipientAttribs);
62+
}
63+
64+
// Secondary 'CC' recipients with the primary recipients listed in 'To:' header
65+
String toHeader = stringArrayToCSV(toRecipients);
66+
for (String cc : ccRecipients) {
67+
RecipientAttributes recipientAttribs = new RecipientAttributes();
68+
AddressAttributes addressAttribs = new AddressAttributes(cc);
69+
addressAttribs.setHeaderTo(toHeader);
70+
recipientAttribs.setAddress(addressAttribs);
71+
recipientArray.add(recipientAttribs);
72+
}
73+
74+
transmission.setRecipientArray(recipientArray);
75+
76+
// Populate Email Body
77+
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
78+
contentAttributes.setFrom(new AddressAttributes(from));
79+
contentAttributes.setSubject("CC Example");
80+
contentAttributes.setText("This message was sent To 1 recipient and some other recipients were CC'd");
81+
contentAttributes.setHtml("<p>This message was sent To 1 recipient and some other recipients were CC'd</p>");
82+
83+
// List the CC recipients in the CC header
84+
Map<String, String> headers = new HashMap<String, String>();
85+
String ccHeader = stringArrayToCSV(ccRecipients);
86+
headers.put("CC", ccHeader);
87+
88+
contentAttributes.setHeaders(headers);
89+
transmission.setContentAttributes(contentAttributes);
90+
91+
// Send the Email
92+
RestConnection connection = new RestConnection(this.client, getEndPoint());
93+
Response response = ResourceTransmissions.create(connection, 0, transmission);
94+
95+
logger.debug("Transmission Response: " + response);
96+
}
97+
}
98+

apps/sparkpost-samples-app/src/main/java/com/sparkpost/sdk/samples/helpers/SparkPostBaseApp.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ protected Client newConfiguredClient() throws SparkPostException, IOException {
5555
return client;
5656
}
5757

58+
protected String getProperty(String name, String defaultValue) {
59+
return properties.getProperty(name, defaultValue);
60+
}
61+
5862
public String getEndPoint() {
5963
String endpoint = this.properties.getProperty("SPARKPOST_BASE_URL", RestConnection.defaultApiEndpoint);
6064

@@ -100,17 +104,40 @@ public String getTemplate(String name) {
100104
}
101105

102106
public String[] getTestRecipients() {
103-
String recipListString = properties.getProperty("SPARKPOST_RECIPIENTS", null);
104-
if (StringUtils.isAnyEmpty(recipListString)) {
105-
throw new IllegalStateException("This sample requires you to fill in `SPARKPOST_RECIPIENTS` in config.properties.");
106-
}
107-
108-
String[] results = recipListString.split(",");
109-
return results;
107+
return getRecipientListProperty("SPARKPOST_RECIPIENTS");
110108
}
111109

112110
public List<String> getTestRecipientsAsList() {
113111
return Arrays.asList(getTestRecipients());
114112
}
115113

114+
public String[] getCCRecipients() {
115+
return getRecipientListProperty("SPARKPOST_CC_RECIPIENTS");
116+
}
117+
118+
public String[] getBCCRecipients() {
119+
return getRecipientListProperty("SPARKPOST_BCC_RECIPIENTS");
120+
}
121+
122+
public String stringArrayToCSV(String[] lst) {
123+
StringBuilder result = new StringBuilder();
124+
for (int idx = 0; idx < lst.length; ++idx) {
125+
result.append(lst[idx]);
126+
if (idx < lst.length-1) {
127+
result.append(",");
128+
}
129+
}
130+
return result.toString();
131+
}
132+
133+
private String[] getRecipientListProperty(String propName) {
134+
String recipListString = getProperty(propName, null);
135+
if (StringUtils.isAnyEmpty(recipListString)) {
136+
throw new IllegalStateException("This sample requires you to fill in `" + propName + "` in config.properties.");
137+
}
138+
139+
String[] results = recipListString.split(",");
140+
return results;
141+
}
116142
}
143+

0 commit comments

Comments
 (0)