Skip to content

Messages

Igor Balos edited this page Nov 13, 2017 · 7 revisions

Sending email with Postmark is easy. All you need to do is create a message you plan to send and send it with API client.

Here are couple of simple sending examples.

Message message = new Message("from@example.com", "matt@example.com", "Hello world simple", "Hello body");
message.setReplyTo("igor+reply@example.com");

// Deliver a simple message
MessageResponse response = client.deliverMessage(message);
message = new Message("from@example.com", "igor@example.com", "Hello world with tag", "Hello body");
message.setTag("testTag");

// Deliver a simple message with tag
response = client.deliverMessage(message);

You can enable open and click tracking super easy too. Check out the example below.

String htmlMessage = "<!DOCTYPE html><html><body>" +
"<p>Hello<a href=\"http://www.google.com\">world</a></p></body></html>";

message = new Message("from@example.com", "nick@example.com", "Hello world - opens and clicks", htmlMessage);
message.setTrackOpens(true);
message.setTrackLinks(Message.TRACK_LINKS.HtmlAndText);

// Deliver a simple message with click and open tracking
response = client.deliverMessage(message);

In order to send emails with attachments, all you need to do is add attachment with specifying path where its located in your message.

message = new Message("from@example.com", "matt@example.com", "Hello world - attachment", "Hello body");
message.addAttachment("/Users/bash/Documents/Temp/test.rb");
response = client.deliverMessage(message);
// Deliver message with attachments

You can add custom headers in two easy ways. Check them out in the example.

message = new Message("from@example.com", "matt@example.com", "Hello world - headers", "Hello body");

ArrayList<Header> headers = new ArrayList<>();
headers.add(new Header("TEST", "CUSTOM HEADER"));
message.setHeaders(headers);

message.addHeader("TEST2", "CUSTOM HEADER2");
response = client.deliverMessage(message);

Sending email to many recipients and include their full names too.

message = new Message("from@example.com", "chris@example.com", "Hello world - recipients", "Hello body");

HashMap<String, String> to = new HashMap<>();
to.put("John Smith", "igor+john@wildbit.com");
to.put("Igor Smith", "igor+from@example.com");
HashMap<String, String> cc = new HashMap<>();
cc.put("Igor Smith", "matt@example.com");
message.setTo(to);
message.setCc(cc);

// Deliver message with full name recipients
response = client.deliverMessage(message);
message = new Message("from@example.com", "matt@example.com", "Hello world - recipients", "Hello body");

ArrayList<String> to = new ArrayList<>();
to.add("from@example.com");
to.add("matt@example.com");
message.setTo(to);

// Deliver message with array of recipients
response = client.deliverMessage(message);

The library also allows you to send emails as batches. This allows you to send many emails at once. All you need to do is specify an array of messages you plan to send and send them.

ArrayList<Message> messages = new ArrayList<>();
messages.add(new Message("from@example.com", "matt@example.com", "Hello world - message one", "Hello body"));
messages.add(new Message("from@example.com", "matt@example.com", "Hello world - message two", "Hello body"));

// Deliver messages by batch
ArrayList<MessageResponse> response = client.deliverMessage(messages);

Clone this wiki locally