Skip to content

Commit 1d37d07

Browse files
committed
Merge pull request #29 from SparkPost/feature/issue_26_attachments
Feature/issue 26 attachments
2 parents c0e66bd + 26bfb18 commit 1d37d07

File tree

15 files changed

+239
-35
lines changed

15 files changed

+239
-35
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The SparkPost Java Library is available in this [Maven Repository](http://maven.
2323
<dependency>
2424
<groupId>com.sparkpost</groupId>
2525
<artifactId>sparkpost-lib</artifactId>
26-
<version>0.13</version>
26+
<version>0.14</version>
2727
</dependency>
2828
```
2929

@@ -59,7 +59,7 @@ public class SparkPost {
5959

6060
## Advanced Send Email Example
6161

62-
With SparkPost you have complete control over all aspects of an email and a powerful tempting solution.
62+
With SparkPost you have complete control over all aspects of an email and a powerful templating solution.
6363

6464
```java
6565

@@ -75,17 +75,18 @@ private void sendEmail(String from, String[] recipients, String email) throws Sp
7575
}
7676
transmission.setRecipientArray(recipientArray);
7777

78-
transmission.setReturnPath(from);
79-
80-
// Populate Substitution Data
81-
Map<String, String> substitutionData = new HashMap<String, String>();
82-
substitutionData.put("from", from);
83-
transmission.setSubstitutionData(substitutionData);
84-
85-
// Populate Email Body
86-
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
87-
contentAttributes.setEmailRFC822(email);
88-
transmission.setContentAttributes(contentAttributes);
78+
// Populate Substitution Data
79+
Map<String, Object> substitutionData = new HashMap<String, Object>();
80+
substitutionData.put("yourContent", "You can add substitution data too.");
81+
transmission.setSubstitutionData(substitutionData);
82+
83+
// Populate Email Body
84+
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
85+
contentAttributes.setFrom(new AddressAttributes(from));
86+
contentAttributes.setSubject("Your subject content here. {{yourContent}}");
87+
contentAttributes.setText("Your Text content here. {{yourContent}}");
88+
contentAttributes.setHtml("<p>Your <b>HTML</b> content here. {{yourContent}}</p>");
89+
transmission.setContentAttributes(contentAttributes);
8990

9091
// Send the Email
9192
RestConnection connection = new RestConnection(client, getEndPoint());

apps/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>com.sparkpost</groupId>
55
<artifactId>sparkpost</artifactId>
6-
<version>0.13</version>
6+
<version>0.14</version>
77
</parent>
88
<modelVersion>4.0.0</modelVersion>
99
<artifactId>apps</artifactId>

apps/sparkpost-documentor-app/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.sparkpost</groupId>
66
<artifactId>apps</artifactId>
7-
<version>0.13</version>
7+
<version>0.14</version>
88
</parent>
99
<artifactId>sparkpost-documentor-app</artifactId>
1010
<name>Generates Markdown of Protocol</name>

apps/sparkpost-javamail-app/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.sparkpost</groupId>
88
<artifactId>apps</artifactId>
9-
<version>0.13</version>
9+
<version>0.14</version>
1010
</parent>
1111
<groupId>com.sparkpost.sample</groupId>
1212
<artifactId>sparkpost-javamail-app</artifactId>

apps/sparkpost-samples-app/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.sparkpost</groupId>
66
<artifactId>apps</artifactId>
7-
<version>0.13</version>
7+
<version>0.14</version>
88
</parent>
99
<artifactId>sparkpost-samples-app</artifactId>
1010
<name>Example use SparkPost library</name>

apps/sparkpost-samples-app/src/main/java/com/sparkpost/samples/SendEmailSample.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,14 @@ private void runApp() throws SparkPostException, IOException {
3838
this.client = this.newConfiguredClient();
3939

4040
// Loads an email to send from the file system
41-
String template = getTemplate("sample_sp_email.eml");
4241
String fromAddress = getFromAddress();
4342
String[] recipients = getTestRecipients();
4443

45-
sendEmail(fromAddress, recipients, template);
44+
sendEmail(fromAddress, recipients);
4645

4746
}
4847

49-
private void sendEmail(String from, String[] recipients, String email) throws SparkPostException {
48+
private void sendEmail(String from, String[] recipients) throws SparkPostException {
5049
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();
5150

5251
// Populate Recipients
@@ -58,16 +57,19 @@ private void sendEmail(String from, String[] recipients, String email) throws Sp
5857
}
5958
transmission.setRecipientArray(recipientArray);
6059

61-
transmission.setReturnPath(from);
62-
6360
// Populate Substitution Data
6461
Map<String, Object> substitutionData = new HashMap<String, Object>();
65-
substitutionData.put("from", from);
62+
substitutionData.put("yourContent", "You can add substitution data too.");
6663
transmission.setSubstitutionData(substitutionData);
6764

6865
// Populate Email Body
6966
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
70-
contentAttributes.setEmailRFC822(email);
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+
transmission.setContentAttributes(contentAttributes);
72+
7173
transmission.setContentAttributes(contentAttributes);
7274

7375
// Send the Email
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}

libs/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.sparkpost</groupId>
66
<artifactId>sparkpost</artifactId>
7-
<version>0.13</version>
7+
<version>0.14</version>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010
<artifactId>libs</artifactId>

libs/sparkpost-lib/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.sparkpost</groupId>
66
<artifactId>libs</artifactId>
7-
<version>0.13</version>
7+
<version>0.14</version>
88
</parent>
99
<!-- <version>0.10</version> -->
1010
<artifactId>sparkpost-lib</artifactId>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
package com.sparkpost.model;
3+
4+
import lombok.Data;
5+
import lombok.EqualsAndHashCode;
6+
7+
/**
8+
* DTO for a transmission of attachments.
9+
*/
10+
@Data
11+
@EqualsAndHashCode(callSuper = true)
12+
public class AttachmentAttributes extends FileAttributes {
13+
14+
}

0 commit comments

Comments
 (0)