GithubHelp home page GithubHelp logo

marcgs / java-sparkpost Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sparkpost/java-sparkpost

0.0 2.0 0.0 548 KB

SparkPost client library for Java

Home Page: https://www.sparkpost.com/

License: Other

Java 97.31% HTML 1.24% Shell 1.45%

java-sparkpost's Introduction

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.

Version Compatibility Note

Version 0.6.2 -> 0.6.3

Due to issue 57 and to maintain compatibility with old and new version of Apache HTTP Client SPARKPOST_BASE_URL must not end with a / slash.

Version 0.12 -> 0.13

Although we try to maintain library backward compatibility this migration may require some minor changes to your code. Substitution data was changed from Map<String, String> to Map<String, Object>. Most client code will just need to change their map to this new signature.

Getting Started

The SparkPost Java Library is available in this Maven Repository or in GitHub Releases.

<dependency>
	<groupId>com.sparkpost</groupId>
	<artifactId>sparkpost-lib</artifactId>
	<version>0.19</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(
                "[email protected]",
                "[email protected]",
                "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 templating 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);

	 // Populate Substitution Data
    Map<String, Object> substitutionData = new HashMap<String, Object>();
    substitutionData.put("yourContent", "You can add substitution data too.");
    transmission.setSubstitutionData(substitutionData);

    // Populate Email Body
    TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
    contentAttributes.setFrom(new AddressAttributes(from));
    contentAttributes.setSubject("Your subject content here. {{yourContent}}");
    contentAttributes.setText("Your Text content here.  {{yourContent}}");
    contentAttributes.setHtml("<p>Your <b>HTML</b> content here.  {{yourContent}}</p>");
    transmission.setContentAttributes(contentAttributes);

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

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

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:

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:

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

java-sparkpost's People

Contributors

aleksandrbeljakov avatar aydrian avatar bdeanindy avatar dominicgunn avatar ewandennis avatar guirava avatar kalnik-a-a avatar richleland avatar svenwoltmann avatar yepher avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.