-
Notifications
You must be signed in to change notification settings - Fork 35
/
SendInvalidFromAddress.java
94 lines (72 loc) · 3.65 KB
/
SendInvalidFromAddress.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
93
94
package com.sparkpost.error.samples;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import com.sparkpost.Client;
import com.sparkpost.exception.SparkPostErrorServerResponseException;
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.ServerErrorResponse;
import com.sparkpost.resources.ResourceTransmissions;
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
import com.sparkpost.transport.RestConnection;
public class SendInvalidFromAddress extends SparkPostBaseApp {
static final Logger logger = Logger.getLogger(SendInvalidFromAddress.class);
private Client client;
public static void main(String[] args) throws SparkPostException, IOException {
Logger.getRootLogger().setLevel(Level.DEBUG);
SendInvalidFromAddress sample = new SendInvalidFromAddress();
sample.runApp();
}
private void runApp() throws SparkPostException, IOException {
this.client = this.newConfiguredClient();
// Loads an email to send from the file system
String fromAddress = "invalid@sparkpost.com";
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>");
transmission.setContentAttributes(contentAttributes);
transmission.setContentAttributes(contentAttributes);
// Send the Email
try {
RestConnection connection = new RestConnection(this.client, getEndPoint());
ResourceTransmissions.create(connection, 0, transmission);
throw new IllegalStateException("Error: Expected Exception for invalid to address");
} catch (SparkPostErrorServerResponseException e) {
System.out.println("GOOD: we got the expected exception");
System.out.println(e.getMessage());
if (e.getServerErrorResponses() != null) {
for (ServerErrorResponse error : e.getServerErrorResponses().getErrors()) {
System.out.println("ERROR: " + error);
}
}
}
}
}