|
| 1 | + |
| 2 | +package com.sparkpost.error.samples; |
| 3 | + |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.apache.log4j.Level; |
| 11 | +import org.apache.log4j.Logger; |
| 12 | + |
| 13 | +import com.sparkpost.Client; |
| 14 | +import com.sparkpost.exception.SparkPostErrorServerResponseException; |
| 15 | +import com.sparkpost.exception.SparkPostException; |
| 16 | +import com.sparkpost.model.AddressAttributes; |
| 17 | +import com.sparkpost.model.RecipientAttributes; |
| 18 | +import com.sparkpost.model.TemplateContentAttributes; |
| 19 | +import com.sparkpost.model.TransmissionWithRecipientArray; |
| 20 | +import com.sparkpost.model.responses.ServerErrorResponse; |
| 21 | +import com.sparkpost.resources.ResourceTransmissions; |
| 22 | +import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp; |
| 23 | +import com.sparkpost.transport.RestConnection; |
| 24 | + |
| 25 | +public class SendInvalidFromAddress extends SparkPostBaseApp { |
| 26 | + |
| 27 | + static final Logger logger = Logger.getLogger(SendInvalidFromAddress.class); |
| 28 | + |
| 29 | + private Client client; |
| 30 | + |
| 31 | + public static void main(String[] args) throws SparkPostException, IOException { |
| 32 | + Logger.getRootLogger().setLevel(Level.DEBUG); |
| 33 | + |
| 34 | + SendInvalidFromAddress sample = new SendInvalidFromAddress(); |
| 35 | + sample.runApp(); |
| 36 | + } |
| 37 | + |
| 38 | + private void runApp() throws SparkPostException, IOException { |
| 39 | + this.client = this.newConfiguredClient(); |
| 40 | + |
| 41 | + // Loads an email to send from the file system |
| 42 | + String fromAddress = "invalid@sparkpost.com"; |
| 43 | + String[] recipients = getTestRecipients(); |
| 44 | + |
| 45 | + sendEmail(fromAddress, recipients); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + private void sendEmail(String from, String[] recipients) throws SparkPostException { |
| 50 | + TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray(); |
| 51 | + |
| 52 | + // Populate Recipients |
| 53 | + List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>(); |
| 54 | + for (String recipient : recipients) { |
| 55 | + RecipientAttributes recipientAttribs = new RecipientAttributes(); |
| 56 | + recipientAttribs.setAddress(new AddressAttributes(recipient)); |
| 57 | + recipientArray.add(recipientAttribs); |
| 58 | + } |
| 59 | + transmission.setRecipientArray(recipientArray); |
| 60 | + |
| 61 | + // Populate Substitution Data |
| 62 | + Map<String, Object> substitutionData = new HashMap<String, Object>(); |
| 63 | + substitutionData.put("yourContent", "You can add substitution data too."); |
| 64 | + transmission.setSubstitutionData(substitutionData); |
| 65 | + |
| 66 | + // Populate Email Body |
| 67 | + TemplateContentAttributes contentAttributes = new TemplateContentAttributes(); |
| 68 | + contentAttributes.setFrom(new AddressAttributes(from)); |
| 69 | + contentAttributes.setSubject("☰ Your subject content here. {{yourContent}}"); |
| 70 | + contentAttributes.setText("Your Text content here. {{yourContent}}"); |
| 71 | + contentAttributes.setHtml("<p>Your <b>HTML</b> content here. {{yourContent}}</p>"); |
| 72 | + transmission.setContentAttributes(contentAttributes); |
| 73 | + |
| 74 | + transmission.setContentAttributes(contentAttributes); |
| 75 | + |
| 76 | + // Send the Email |
| 77 | + try { |
| 78 | + RestConnection connection = new RestConnection(this.client, getEndPoint()); |
| 79 | + ResourceTransmissions.create(connection, 0, transmission); |
| 80 | + |
| 81 | + throw new IllegalStateException("Error: Expected Exception for invalid to address"); |
| 82 | + } catch (SparkPostErrorServerResponseException e) { |
| 83 | + System.out.println("GOOD: we got the expected exception"); |
| 84 | + System.out.println(e.getMessage()); |
| 85 | + |
| 86 | + if (e.getServerErrorResponses() != null) { |
| 87 | + for (ServerErrorResponse error : e.getServerErrorResponses().getErrors()) { |
| 88 | + System.out.println("ERROR: " + error); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments