diff --git a/README.md b/README.md index 40eaf57..75c200f 100644 --- a/README.md +++ b/README.md @@ -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 +``` + diff --git a/apps/sparkpost-samples-app/config.properties.example b/apps/sparkpost-samples-app/config.properties.example index 14d47d4..33b734f 100644 --- a/apps/sparkpost-samples-app/config.properties.example +++ b/apps/sparkpost-samples-app/config.properties.example @@ -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 diff --git a/apps/sparkpost-samples-app/src/main/java/com/sparkpost/samples/SendEmailBCCSample.java b/apps/sparkpost-samples-app/src/main/java/com/sparkpost/samples/SendEmailBCCSample.java new file mode 100644 index 0000000..4c53d25 --- /dev/null +++ b/apps/sparkpost-samples-app/src/main/java/com/sparkpost/samples/SendEmailBCCSample.java @@ -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 recipientArray = new ArrayList(); + + // 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("

This message was sent To 1 recipient and some other recipients were quietly BCCd

"); + + 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); + } + +} + diff --git a/apps/sparkpost-samples-app/src/main/java/com/sparkpost/samples/SendEmailCCSample.java b/apps/sparkpost-samples-app/src/main/java/com/sparkpost/samples/SendEmailCCSample.java new file mode 100644 index 0000000..f557f18 --- /dev/null +++ b/apps/sparkpost-samples-app/src/main/java/com/sparkpost/samples/SendEmailCCSample.java @@ -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 recipientArray = new ArrayList(); + + // 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("

This message was sent To 1 recipient and some other recipients were CC'd

"); + + // List the CC recipients in the CC header + Map headers = new HashMap(); + 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); + } +} + diff --git a/apps/sparkpost-samples-app/src/main/java/com/sparkpost/sdk/samples/helpers/SparkPostBaseApp.java b/apps/sparkpost-samples-app/src/main/java/com/sparkpost/sdk/samples/helpers/SparkPostBaseApp.java index cf7be7e..059a27e 100644 --- a/apps/sparkpost-samples-app/src/main/java/com/sparkpost/sdk/samples/helpers/SparkPostBaseApp.java +++ b/apps/sparkpost-samples-app/src/main/java/com/sparkpost/sdk/samples/helpers/SparkPostBaseApp.java @@ -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); @@ -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 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; + } } +