-
Notifications
You must be signed in to change notification settings - Fork 35
/
SendEmailSample.java
92 lines (70 loc) · 3.53 KB
/
SendEmailSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.sparkpost.samples;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import com.sparkpost.Client;
import com.sparkpost.exception.SparkPostException;
import com.sparkpost.model.AddressAttributes;
import com.sparkpost.model.RecipientAttributes;
import com.sparkpost.model.TemplateContentAttributes;
import com.sparkpost.model.TransmissionWithRecipientArray;
import com.sparkpost.model.responses.Response;
import com.sparkpost.resources.ResourceTransmissions;
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
import com.sparkpost.transport.IRestConnection;
import com.sparkpost.transport.RestConnection;
public class SendEmailSample extends SparkPostBaseApp {
static final Logger logger = LogManager.getLogger(CreateTemplateSimple.class);
private Client client;
public static void main(String[] args) throws SparkPostException, IOException {
Configurator.setRootLevel(Level.DEBUG);
SendEmailSample sample = new SendEmailSample();
sample.runApp();
}
private void runApp() throws SparkPostException, IOException {
this.client = this.newConfiguredClient();
// Loads an email to send from the file system
String fromAddress = getFromAddress();
String[] recipients = getTestRecipients();
sendEmail(fromAddress, recipients);
}
private void sendEmail(String from, String[] recipients) 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>");
//////////////////////
// Add custom email header to demonstrate how it is done
Map<String, String> headers = new HashMap<String, String>();
headers.put("Message-ID", "<message-id-test@montes.co>");
headers.put("MyKey", "Stuff for the key");
contentAttributes.setHeaders(headers);
//////////////////////
transmission.setContentAttributes(contentAttributes);
// Send the Email
IRestConnection connection = new RestConnection(this.client, getEndPoint());
Response response = ResourceTransmissions.create(connection, 0, transmission);
logger.debug("Transmission Response: " + response);
}
}