Skip to content

Commit 536d58b

Browse files
committed
Merge pull request #49 from SparkPost/FAD-3059
Added CC and BCC samples
2 parents 3287ac9 + 20f8dbc commit 536d58b

File tree

5 files changed

+244
-7
lines changed

5 files changed

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

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)