|
| 1 | + |
| 2 | +package com.sparkpost.samples; |
| 3 | + |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.apache.log4j.Level; |
| 9 | +import org.apache.log4j.Logger; |
| 10 | + |
| 11 | +import com.sparkpost.Client; |
| 12 | +import com.sparkpost.exception.SparkPostException; |
| 13 | +import com.sparkpost.model.AddressAttributes; |
| 14 | +import com.sparkpost.model.AttachmentAttributes; |
| 15 | +import com.sparkpost.model.InlineImageAttributes; |
| 16 | +import com.sparkpost.model.RecipientAttributes; |
| 17 | +import com.sparkpost.model.TemplateContentAttributes; |
| 18 | +import com.sparkpost.model.TransmissionWithRecipientArray; |
| 19 | +import com.sparkpost.model.responses.Response; |
| 20 | +import com.sparkpost.resources.ResourceTransmissions; |
| 21 | +import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp; |
| 22 | +import com.sparkpost.transport.RestConnection; |
| 23 | + |
| 24 | +public class SendEmailWithFilesSample extends SparkPostBaseApp { |
| 25 | + |
| 26 | + static final Logger logger = Logger.getLogger(CreateTemplateSimple.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 | + SendEmailWithFilesSample sample = new SendEmailWithFilesSample(); |
| 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 Email Body |
| 61 | + TemplateContentAttributes contentAttributes = new TemplateContentAttributes(); |
| 62 | + contentAttributes.setFrom(new AddressAttributes(from)); |
| 63 | + contentAttributes.setSubject("Hello World"); |
| 64 | + contentAttributes.setText("Simple text content"); |
| 65 | + |
| 66 | + // Add a text attachment |
| 67 | + AttachmentAttributes attachment = new AttachmentAttributes(); |
| 68 | + attachment.setName("aFile.txt"); |
| 69 | + attachment.setType("text/plain; charset=UTF-8;"); |
| 70 | + // This is Base64 of the file contents |
| 71 | + attachment.setData("SGVsbG8gV29ybGQhCuydvA=="); |
| 72 | + List<AttachmentAttributes> attachments = new ArrayList<>(); |
| 73 | + attachments.add(attachment); |
| 74 | + contentAttributes.setAttachments(attachments); |
| 75 | + |
| 76 | + // Add inline image |
| 77 | + InlineImageAttributes image = new InlineImageAttributes(); |
| 78 | + /* |
| 79 | + * The name of the inline image, which will be inserted into the Content-ID header. |
| 80 | + * The image should be referenced in your HTML content using <img src="cid:THIS_NAME">. |
| 81 | + * The name must be unique within the content.inline_images array. |
| 82 | + */ |
| 83 | + contentAttributes.setHtml("<p>My fantastic HTML content.<br><br><b>SparkPost</b> <img src=\"cid:AnImage.png\"></p>"); |
| 84 | + image.setName("AnImage.png"); |
| 85 | + image.setType("image/png"); |
| 86 | + // This is Base64 of the file contents |
| 87 | + image.setData( |
| 88 | + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAWJQAAFiUBSVIk8AAAAXxJREFUOBFjvJVg84P5718WBjLAX2bmPyxMf/+xMDH8YyZDPwPDXwYGJkIaOXTNGdiUtHAqI2jA/18/GUQzGsg3gMfKg4FVQo6BiYcPqyF4XcChaczA4+DP8P//f4b/P3+SZgAzvxCDSGYjAyMjI8PvZw+AoYXdLuyiQLtE0uoZWAREwLb+fnKXQTipkngXcJu7MnACQx8G2FX1GHgs3bDGBlYX8HlFM/z9+JbhzewWhmf1CQyfti9j+PfzBwO/ZxTMTDiNmQKBfmZX1GB42V/K8P38YbDCX/dvMDAwMzPwuYbBNcIYmC4AhfjvXwx/376AqQHTf96+ZPj34xuKGIiDaQBQ8PPBTQwCoZkMjJzcYA3MgqIMAr7xDJ/3rAHzkQnGO7FWf5gZ/qLmBSZmBoHgNAZee1+Gf18/MzCyczJ83LyQ4fPetch6Gf4xMP3FbgBMGdAgJqAr/n37zABMTTBROA0ygAWUJUG5Civ4B8xwX78CpbD6FJiHmf4AAFicbTMTr5jAAAAAAElFTkSuQmCC"); |
| 89 | + List<InlineImageAttributes> inlineImages = new ArrayList<InlineImageAttributes>(); |
| 90 | + inlineImages.add(image); |
| 91 | + contentAttributes.setInlineImages(inlineImages); |
| 92 | + |
| 93 | + transmission.setContentAttributes(contentAttributes); |
| 94 | + |
| 95 | + // Send the Email |
| 96 | + RestConnection connection = new RestConnection(this.client, getEndPoint()); |
| 97 | + Response response = ResourceTransmissions.create(connection, 0, transmission); |
| 98 | + |
| 99 | + logger.debug("Transmission Response: " + response); |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments