Giter Club home page Giter Club logo

socketlabs-java's Introduction

SocketLabs

Twitter Follow MIT licensed PRs Welcome

The SocketLabs Email Delivery Java library allows you to easily send email messages via the SocketLabs Injection API. The library makes it easy to build and send any type of message supported by the API, from a simple message to a single recipient all the way to a complex bulk message sent to a group of recipients with unique merge data per recipient.

Table of Contents

Prerequisites and Installation

Prerequisites

  • A supported Java version
    • Oracle JDK 8
  • A SocketLabs account. If you don't have one yet, you can sign up for a free account to get started.

Installation

Choose your installation method - Maven w/ Gradle (recommended), Maven or Jar file.

via Maven w/ Gradle

Add the following to your build.gradle file in the root of your project.

...
dependencies {
  ...
  compile group: 'com.socketlabs', name: 'injectionApi', version: '1.0.4'
}

repositories {
  mavenCentral()
}
...

via Maven

<dependency>
  <groupId>com.socketlabs</groupId>
  <artifactId>injectionApi</artifactId>
  <version>1.0.4</version>
</dependency>

via jar file

You can just drop the jar file in.

injectionApi-1.0.4.jar

injectionApi-1.0.4-sources.jar

injectionApi-1.0.4-javadoc.jar

Alternately, you can simply clone this repository directly to include the source code in your project.

Getting Started

Obtaining your API Key and SocketLabs ServerId number

In order to get started, you'll need to enable the Injection API feature in the SocketLabs Control Panel. Once logged in, navigate to your SocketLabs server's dashboard (if you only have one server on your account you'll be taken here immediately after logging in). Make note of your 4 or 5 digit ServerId number, as you'll need this along with your API key in order to use the Injection API.

To enable the Injection API, click on the "For Developers" dropdown on the top-level navigation, then choose the "Configure HTTP Injection API" option. Once here, you can enable the feature by choosing the "Enabled" option in the dropdown. Enabling the feature will also generate your API key, which you'll need (along with your ServerId) to start using the API. Be sure to click the "Update" button to save your changes once you are finished.

Basic Message

A basic message is an email message like you'd send from a personal email client such as Outlook. A basic message can have many recipients, including multiple To addresses, CC addresses, and even BCC addresses. You can also send a file attachment in a basic message.

import com.socketLabs.injectionApi.*;
import com.socketLabs.injectionApi.message.*;

SocketLabsClient client = new SocketLabsClient(000001, "YOUR-API-KEY"); //Your SocketLabs ServerId and Injection API key

BasicMessage message = new BasicMessage();

message.setSubject("Sending A Test Message (Basic Send)");
message.setHtmlBody("<html><body><h1>Sending A Test Message</h1><p>This is the Html Body of my message.</p></body></html>");
message.setPlainTextBody("This is the Plain Text Body of my message.");

message.setFrom(new EmailAddress("[email protected]"));

//A basic message supports up to 50 recipients and supports several different ways to add recipients

// Adding To Recipients
message.getTo().add(new EmailAddress("[email protected]"));
message.getTo().add(new EmailAddress("[email protected]", "Recipient #2"));

// Adding CC Recipients
message.addCcEmailAddress(new EmailAddress("[email protected]",  "Recipient #3" ));

 // Adding Bcc Recipients
message.addBccEmailAddress("[email protected]");
message.addBccEmailAddress("[email protected]", "Recipient #2");

SendResponse response =  client.send(message);

Bulk Message

A bulk message usually contains a single recipient per message and is generally used to send the same content to many recipients, optionally customizing the message via the use of MergeData. For more information about using Merge data, please see the Injection API documentation.

import com.socketLabs.injectionApi.*;
import com.socketLabs.injectionApi.message.*;

SocketLabsClient client = new SocketLabsClient(000001, "YOUR-API-KEY"); //Your SocketLabs ServerId and Injection API key

BulkMessage message = new BulkMessage();

message.setSubject("Sending A Test Message (Bulk Send)");
message.setHtmlBody("<html><body><h1>Sending A Test Message</h1><p>This is the Html Body of my message.</p></body></html>");
message.setPlainTextBody("This is the Plain Text Body of my message.");

message.setFrom(new EmailAddress("[email protected]"));
message.setReplyTo(new EmailAddress("[email protected]"));

message.getTo().add(new BulkRecipient("[email protected]"));
message.getTo().add(new BulkRecipient("[email protected]", "Recipient #2"));
message.getTo().add(new BulkRecipient("[email protected]"));
message.getTo().add(new BulkRecipient("[email protected]", "Recipient #4"));

SendResponse response =  client.send(message);

Managing API Keys

For ease of demonstration, many of our examples include the ServerId (SOCKETLABS_SERVER_ID) and API key (SOCKETLABS_INJECTION_API_KEY) directly in our code sample. Generally it is not considered a good practice to store sensitive information like this directly in your code. Depending on your project type, we recommend either storing your credentials using Environment Variables. For more information please see: Using Environment Variables

Examples and Use Cases

In order to demonstrate the many possible use cases for the SDK, we've provided an assortment of code examples. These examples demonstrate many different features available to the Injection API and SDK, including using templates created in the SocketLabs Email Designer, custom email headers, sending attachments, sending content that is stored in an HTML file, advanced bulk merging, and even pulling recipients from a datasource.

This example demonstrates a Basic Send done asynchronously.

This example demonstrates a Basic Send done synchronously.

This example demonstrates many features of the Basic Send, including adding multiple recipients, adding message and mailing id's, and adding an embedded image.

This example demonstrates how to read in your HTML content from an HTML file rather than passing in a string directly.

This example demonstrates the sending of a piece of content that was created in the SocketLabs Email Designer. This is also known as the API Templates feature.

This example demonstrates sending with a specific character set.

This example demonstrates how to add a file attachment to your message.

This example demonstrates how to add custom headers to your email message.

This example demonstrates how to embed an image in your message.

This example demonstrates how to use a proxy with your HTTP client.

This example demonstrates the results of attempting to do a send with an invalid attachment.

This example demonstrates the results of attempting to do a send with an invalid from address.

This example demonstrates the results of attempting to do a send with invalid recipients.

This example demonstrates how to send a bulk message to multiple recipients.

This example demonstrates many features of the BulkMessage(), including adding multiple recipients, merge data, and adding an attachment.

This example uses a mock repository class to demonstrate how you would pull your recipients from a database and create a bulk mailing with merge data.

This example demonstrates how to send a bulk message with a specified character set and special characters.

This example demonstrates how to send a bulk message to multiple recipients with unique merge data per recipient.

License

The SocketLabs.EmailDelivery library and all associated code, including any code samples, are MIT Licensed.

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.