|
| 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.resources.ResourceTransmissions; |
| 21 | +import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp; |
| 22 | +import com.sparkpost.transport.RestConnection; |
| 23 | + |
| 24 | +public class SendEmailErrorSample extends SparkPostBaseApp { |
| 25 | + |
| 26 | + static final Logger logger = Logger.getLogger(SendEmailErrorSample.class); |
| 27 | + |
| 28 | + private Client client; |
| 29 | + |
| 30 | + public static void main(String[] args) throws SparkPostException, IOException { |
| 31 | + Logger.getRootLogger().setLevel(Level.DEBUG); |
| 32 | + |
| 33 | + SendEmailErrorSample sample = new SendEmailErrorSample(); |
| 34 | + sample.runApp(); |
| 35 | + } |
| 36 | + |
| 37 | + private void runApp() throws SparkPostException, IOException { |
| 38 | + this.client = this.newConfiguredClient(); |
| 39 | + |
| 40 | + // Loads an email to send from the file system |
| 41 | + String fromAddress = getFromAddress(); |
| 42 | + String[] recipients = getTestRecipients(); |
| 43 | + |
| 44 | + sendEmail(fromAddress, recipients); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + private void sendEmail(String from, String[] recipients) throws SparkPostException { |
| 49 | + TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray(); |
| 50 | + |
| 51 | + // Populate Recipients |
| 52 | + List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>(); |
| 53 | + for (String recipient : recipients) { |
| 54 | + RecipientAttributes recipientAttribs = new RecipientAttributes(); |
| 55 | + recipientAttribs.setAddress(new AddressAttributes(recipient)); |
| 56 | + recipientArray.add(recipientAttribs); |
| 57 | + } |
| 58 | + transmission.setRecipientArray(recipientArray); |
| 59 | + |
| 60 | + // Populate Substitution Data |
| 61 | + Map<String, Object> substitutionData = new HashMap<String, Object>(); |
| 62 | + substitutionData.put("yourContent", "You can add substitution data too."); |
| 63 | + transmission.setSubstitutionData(substitutionData); |
| 64 | + |
| 65 | + // Populate Email Body |
| 66 | + TemplateContentAttributes contentAttributes = new TemplateContentAttributes(); |
| 67 | + contentAttributes.setFrom(new AddressAttributes(from)); |
| 68 | + contentAttributes.setSubject("Your subject content here. {{yourContent}}"); |
| 69 | + contentAttributes.setText("Your Text content here. {{yourContent}}"); |
| 70 | + contentAttributes.setHtml("<p>Your <b>HTML</b> content here. {{yourContent}}</p>"); |
| 71 | + |
| 72 | + // It is illegal to set a templateID with content |
| 73 | + contentAttributes.setTemplateId("This_Is_Bad_When_There_Is_Inline_Content"); |
| 74 | + |
| 75 | + transmission.setContentAttributes(contentAttributes); |
| 76 | + |
| 77 | + transmission.setContentAttributes(contentAttributes); |
| 78 | + |
| 79 | + // Send the Email |
| 80 | + RestConnection connection = new RestConnection(this.client, getEndPoint()); |
| 81 | + |
| 82 | + try { |
| 83 | + |
| 84 | + ResourceTransmissions.create(connection, 0, transmission); |
| 85 | + |
| 86 | + throw new IllegalStateException("Error: Expected SparkPostErrorServerResponseException"); |
| 87 | + } catch (SparkPostErrorServerResponseException e) { |
| 88 | + System.out.println("GOOD: we got the expected SparkPostErrorServerResponseException"); |
| 89 | + System.out.println(e.getMessage()); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments