Skip to content

Commit f63ffeb

Browse files
committed
Adds a simplified API for someone who just wants to send a simple email
1 parent 7f0d64b commit f63ffeb

File tree

1 file changed

+44
-8
lines changed
  • libs/sparkpost-lib/src/main/java/com/sparkpost

1 file changed

+44
-8
lines changed

libs/sparkpost-lib/src/main/java/com/sparkpost/Client.java

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11

22
package com.sparkpost;
33

4-
import com.sparkpost.model.AddressAttributes;
5-
4+
import java.util.ArrayList;
65
import java.util.List;
7-
import java.util.Map;
6+
7+
import com.sparkpost.exception.SparkPostException;
8+
import com.sparkpost.model.AddressAttributes;
9+
import com.sparkpost.model.RecipientAttributes;
10+
import com.sparkpost.model.TemplateContentAttributes;
11+
import com.sparkpost.model.TransmissionWithRecipientArray;
12+
import com.sparkpost.model.responses.Response;
13+
import com.sparkpost.resources.ResourceTransmissions;
14+
import com.sparkpost.transport.RestConnection;
815

916
/**
1017
* The Client class stores everything specific to the SparkPost client:<BR>
@@ -34,7 +41,7 @@ public Client(String key) {
3441

3542
/**
3643
* You can create and API Key here <a href="https://app.sparkpost.com/account/credentials">SparkPost</a>
37-
*
44+
*
3845
* @param key
3946
* SparkPost API Key
4047
*/
@@ -47,15 +54,15 @@ public String getAuthKey() {
4754
}
4855

4956
public String getUsername() {
50-
return username;
57+
return this.username;
5158
}
5259

5360
public void setUsername(String username) {
5461
this.username = username;
5562
}
5663

5764
public String getPassword() {
58-
return password;
65+
return this.password;
5966
}
6067

6168
public void setPassword(String password) {
@@ -66,7 +73,7 @@ public void setPassword(String password) {
6673
* @return the fromEmail
6774
*/
6875
public String getFromEmail() {
69-
return fromEmail;
76+
return this.fromEmail;
7077
}
7178

7279
/**
@@ -77,8 +84,37 @@ public void setFromEmail(String fromEmail) {
7784
this.fromEmail = fromEmail;
7885
}
7986

80-
public void sendMessage(String templateId, List<AddressAttributes> recipients, Map<String, String> args) {
87+
public Response sendMessage(String from, String recipient, String subject, String text, String html) throws SparkPostException {
88+
List<String> recipients = new ArrayList<>();
89+
recipients.add(recipient);
90+
return sendMessage(from, recipients, subject, text, html);
91+
}
92+
93+
public Response sendMessage(String from, List<String> recipients, String subject, String text, String html) throws SparkPostException {
94+
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();
95+
96+
List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();
97+
for (String recpient : recipients) {
98+
99+
RecipientAttributes recipientAttribs = new RecipientAttributes();
100+
recipientAttribs.setAddress(new AddressAttributes(recpient));
101+
recipientArray.add(recipientAttribs);
102+
}
103+
transmission.setRecipientArray(recipientArray);
104+
105+
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
106+
107+
contentAttributes.setFrom(new AddressAttributes(from));
108+
109+
contentAttributes.setSubject(subject);
110+
contentAttributes.setHtml(html);
111+
contentAttributes.setText(text);
112+
transmission.setContentAttributes(contentAttributes);
113+
114+
RestConnection connection = new RestConnection(this);
115+
Response response = ResourceTransmissions.create(connection, 0, transmission);
81116

117+
return response;
82118
}
83119

84120
@Override

0 commit comments

Comments
 (0)