Skip to content

SparkPost client library for Java; forked to fix a bug where data string sent to the API was converted to a byte array twice.

License

Notifications You must be signed in to change notification settings

SvenWoltmann/java-sparkpost

 
 

Repository files navigation

Sign up for a SparkPost account and visit our Developer Hub for even more content.

SparkPost Java Library

Build Status Slack Status

Use this library in Java applications to easily access the SparkPost Email API in your application.

Getting Started

The SparkPost Java Library is available in this Maven Repository:

<dependency>
	<groupId>com.sparkpost</groupId>
	<artifactId>sparkpost-lib</artifactId>
	<version>0.10</version>
</dependency>

Building SparkPost4J

Basic Send Email Example

package com.sparkpost;

import com.sparkpost.exception.SparkPostException;

public class SparkPost {

    public static void main(String[] args) throws SparkPostException {
        String API_KEY = "YOUR API KEY HERE!!!";
        Client client = new Client(API_KEY);

        client.sendMessage(
                "you@yourdomain.com",
                "to@sparkpost.com",
                "The subject of the message",
                "The text part of the email",
                "<b>The HTML part of the email</b>");

    }
}

Advanced Send Email Example

With SparkPost you have complete control over all aspects of an email and a powerful tempting solution.

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

	// Populate Recipients
	List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();
	for (String recipient : recipients) {
	RecipientAttributes recipientAttribs = new RecipientAttributes();
		recipientAttribs.setAddress(new AddressAttributes(recipient));
		recipientArray.add(recipientAttribs);
	}
	transmission.setRecipientArray(recipientArray);

	transmission.setReturnPath(from);

	// Populate Substitution Data
	Map<String, String> substitutionData = new HashMap<String, String>();
	substitutionData.put("from", from);
	transmission.setSubstitutionData(substitutionData);

	// Populate Email Body
	TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
	contentAttributes.setEmailRFC822(email);
	transmission.setContentAttributes(contentAttributes);

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

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

About

SparkPost client library for Java; forked to fix a bug where data string sent to the API was converted to a byte array twice.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 97.4%
  • HTML 2.6%