Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,23 @@ private void sendEmail(String from, String[] recipients, String email) throws Sp
}

```

## Running The Sample Apps

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/`.

To build the samples:

```bash
cd apps/sparkpost-samples-app
mvn compile
```

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.

You can now run your chosen sample through maven:

```bash
mvn exec:java -Dexec.mainClass=com.sparkpost.samples.SendEmailCCSample
```

3 changes: 3 additions & 0 deletions apps/sparkpost-samples-app/config.properties.example
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ SPARKPOST_FROM=from_address@example.com
##
SPARKPOST_RECIPIENTS=recipient_one@example.com, recipient_two@example.com

SPARKPOST_CC_RECIPIENTS=cc_recipient_one@example.com, cc_recipient_two@example.com

SPARKPOST_BCC_RECIPIENTS=bcc_recipient_one@example.com, bcc_recipient_two@example.com

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

package com.sparkpost.samples;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import org.apache.commons.lang3.StringUtils;

import com.sparkpost.Client;
import com.sparkpost.exception.SparkPostException;
import com.sparkpost.model.AddressAttributes;
import com.sparkpost.model.RecipientAttributes;
import com.sparkpost.model.TemplateContentAttributes;
import com.sparkpost.model.TransmissionWithRecipientArray;
import com.sparkpost.model.responses.Response;
import com.sparkpost.resources.ResourceTransmissions;
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
import com.sparkpost.transport.RestConnection;

public class SendEmailBCCSample extends SparkPostBaseApp {

static final Logger logger = Logger.getLogger(CreateTemplateSimple.class);

private Client client;

public static void main(String[] args) throws SparkPostException, IOException {
Logger.getRootLogger().setLevel(Level.DEBUG);

SendEmailBCCSample sample = new SendEmailBCCSample();
sample.runApp();
}

private void runApp() throws SparkPostException, IOException {
this.client = this.newConfiguredClient();

// Loads an email to send from the file system
String fromAddress = getFromAddress();
String[] toRecipients = getTestRecipients();
String[] bccRecipients = getBCCRecipients();

sendEmail(fromAddress, toRecipients, bccRecipients);
}

private void sendEmail(String from, String[] toRecipients, String[] bccRecipients) throws SparkPostException {
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();

// Populate Recipients
List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();

// Primary 'To' recipients
for (String to : toRecipients) {
RecipientAttributes recipientAttribs = new RecipientAttributes();
AddressAttributes addressAttribs = new AddressAttributes(to);
recipientAttribs.setAddress(addressAttribs);
recipientArray.add(recipientAttribs);
}

// Secondary 'BCC' recipients with the primary recipients listed in 'To:' header
String toHeader = stringArrayToCSV(toRecipients);
for (String bcc : bccRecipients) {
RecipientAttributes recipientAttribs = new RecipientAttributes();
AddressAttributes addressAttribs = new AddressAttributes(bcc);
addressAttribs.setHeaderTo(toHeader);
recipientAttribs.setAddress(addressAttribs);
recipientArray.add(recipientAttribs);
}

transmission.setRecipientArray(recipientArray);

// Populate Email Body
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
contentAttributes.setFrom(new AddressAttributes(from));
contentAttributes.setSubject("BCC Example");
contentAttributes.setText("This message was sent To 1 recipient and some other recipients were quietly BCCd");
contentAttributes.setHtml("<p>This message was sent To 1 recipient and some other recipients were quietly BCCd</p>");

transmission.setContentAttributes(contentAttributes);

// Send the Email
RestConnection connection = new RestConnection(this.client, getEndPoint());
Response response = ResourceTransmissions.create(connection, 0, transmission);

logger.debug("Transmission Response: " + response);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

package com.sparkpost.samples;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.sparkpost.Client;
import com.sparkpost.exception.SparkPostException;
import com.sparkpost.model.AddressAttributes;
import com.sparkpost.model.RecipientAttributes;
import com.sparkpost.model.TemplateContentAttributes;
import com.sparkpost.model.TransmissionWithRecipientArray;
import com.sparkpost.model.responses.Response;
import com.sparkpost.resources.ResourceTransmissions;
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
import com.sparkpost.transport.RestConnection;

public class SendEmailCCSample extends SparkPostBaseApp {

static final Logger logger = Logger.getLogger(CreateTemplateSimple.class);

private Client client;

public static void main(String[] args) throws SparkPostException, IOException {
Logger.getRootLogger().setLevel(Level.DEBUG);

SendEmailCCSample sample = new SendEmailCCSample();
sample.runApp();
}

private void runApp() throws SparkPostException, IOException {
this.client = this.newConfiguredClient();

// Loads an email to send from the file system
String fromAddress = getFromAddress();
String[] toRecipients = getTestRecipients();
String[] ccRecipients = getCCRecipients();

sendEmail(fromAddress, toRecipients, ccRecipients);
}

private void sendEmail(String from, String[] toRecipients, String[] ccRecipients) throws SparkPostException {
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();

// Populate Recipients
List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();

// Primary 'To' recipients
for (String to : toRecipients) {
RecipientAttributes recipientAttribs = new RecipientAttributes();
AddressAttributes addressAttribs = new AddressAttributes(to);
recipientAttribs.setAddress(addressAttribs);
recipientArray.add(recipientAttribs);
}

// Secondary 'CC' recipients with the primary recipients listed in 'To:' header
String toHeader = stringArrayToCSV(toRecipients);
for (String cc : ccRecipients) {
RecipientAttributes recipientAttribs = new RecipientAttributes();
AddressAttributes addressAttribs = new AddressAttributes(cc);
addressAttribs.setHeaderTo(toHeader);
recipientAttribs.setAddress(addressAttribs);
recipientArray.add(recipientAttribs);
}

transmission.setRecipientArray(recipientArray);

// Populate Email Body
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
contentAttributes.setFrom(new AddressAttributes(from));
contentAttributes.setSubject("CC Example");
contentAttributes.setText("This message was sent To 1 recipient and some other recipients were CC'd");
contentAttributes.setHtml("<p>This message was sent To 1 recipient and some other recipients were CC'd</p>");

// List the CC recipients in the CC header
Map<String, String> headers = new HashMap<String, String>();
String ccHeader = stringArrayToCSV(ccRecipients);
headers.put("CC", ccHeader);

contentAttributes.setHeaders(headers);
transmission.setContentAttributes(contentAttributes);

// Send the Email
RestConnection connection = new RestConnection(this.client, getEndPoint());
Response response = ResourceTransmissions.create(connection, 0, transmission);

logger.debug("Transmission Response: " + response);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ protected Client newConfiguredClient() throws SparkPostException, IOException {
return client;
}

protected String getProperty(String name, String defaultValue) {
return properties.getProperty(name, defaultValue);
}

public String getEndPoint() {
String endpoint = this.properties.getProperty("SPARKPOST_BASE_URL", RestConnection.defaultApiEndpoint);

Expand Down Expand Up @@ -100,17 +104,40 @@ public String getTemplate(String name) {
}

public String[] getTestRecipients() {
String recipListString = properties.getProperty("SPARKPOST_RECIPIENTS", null);
if (StringUtils.isAnyEmpty(recipListString)) {
throw new IllegalStateException("This sample requires you to fill in `SPARKPOST_RECIPIENTS` in config.properties.");
}

String[] results = recipListString.split(",");
return results;
return getRecipientListProperty("SPARKPOST_RECIPIENTS");
}

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

public String[] getCCRecipients() {
return getRecipientListProperty("SPARKPOST_CC_RECIPIENTS");
}

public String[] getBCCRecipients() {
return getRecipientListProperty("SPARKPOST_BCC_RECIPIENTS");
}

public String stringArrayToCSV(String[] lst) {
StringBuilder result = new StringBuilder();
for (int idx = 0; idx < lst.length; ++idx) {
result.append(lst[idx]);
if (idx < lst.length-1) {
result.append(",");
}
}
return result.toString();
}

private String[] getRecipientListProperty(String propName) {
String recipListString = getProperty(propName, null);
if (StringUtils.isAnyEmpty(recipListString)) {
throw new IllegalStateException("This sample requires you to fill in `" + propName + "` in config.properties.");
}

String[] results = recipListString.split(",");
return results;
}
}