https://github.com/sparkpost/java-sparkpost
SparkPost client library for Java
https://github.com/sparkpost/java-sparkpost
client-library email java
Last synced: 9 months ago
JSON representation
SparkPost client library for Java
- Host: GitHub
- URL: https://github.com/sparkpost/java-sparkpost
- Owner: SparkPost
- License: other
- Created: 2015-03-11T00:23:31.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2023-08-23T05:29:00.000Z (over 2 years ago)
- Last Synced: 2025-04-13T12:27:11.076Z (9 months ago)
- Topics: client-library, email, java
- Language: Java
- Homepage: https://www.sparkpost.com/
- Size: 658 KB
- Stars: 39
- Watchers: 62
- Forks: 35
- Open Issues: 30
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[Sign up](https://app.sparkpost.com/join?plan=free-0817?src=Social%20Media&sfdcid=70160000000pqBb&pc=GitHubSignUp&utm_source=github&utm_medium=social-media&utm_campaign=github&utm_content=sign-up) for a SparkPost account and visit our [Developer Hub](https://developers.sparkpost.com) for even more content.
# SparkPost Java Library
[](https://travis-ci.org/SparkPost/java-sparkpost) [](http://slack.sparkpost.com)
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](https://github.com/SparkPost/java-sparkpost/issues/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` to `Map`. 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](https://repo.maven.apache.org/maven2/com/sparkpost/sparkpost-lib) or in GitHub [Releases](https://github.com/SparkPost/java-sparkpost/releases).
```xml
com.sparkpost
sparkpost-lib
0.27
```
## Building SparkPost4J
## Basic Send Email Example
```java
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",
"The HTML part of the email");
}
}
```
## Basic Send Email through SparkPost EU
```java
package com.sparkpost;
import com.sparkpost.exception.SparkPostException;
import com.sparkpost.transport.IRestConnection;
public class SparkPost {
public static void main(String[] args) throws SparkPostException {
String API_KEY = "YOUR API KEY HERE!!!";
// To use the SparkPost EU use the IRestConnection.SPC_EU_ENDPOINT endpoint
Client client = new Client(API_KEY, IRestConnection.SPC_EU_ENDPOINT);
client.sendMessage(
"you@yourdomain.com",
"to@sparkpost.com",
"The subject of the message",
"The text part of the email",
"The HTML part of the email");
}
}
```
## Advanced Send Email Example
With SparkPost you have complete control over all aspects of an email and a powerful templating solution.
```java
private void sendEmail(String from, String[] recipients, String email) throws SparkPostException {
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();
// Populate Recipients
List recipientArray = new ArrayList();
for (String recipient : recipients) {
RecipientAttributes recipientAttribs = new RecipientAttributes();
recipientAttribs.setAddress(new AddressAttributes(recipient));
recipientArray.add(recipientAttribs);
}
transmission.setRecipientArray(recipientArray);
// Populate Substitution Data
Map substitutionData = new HashMap();
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("
Your HTML content here. {{yourContent}}
");
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:
```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
```