Skip to content

Commit 25d33e7

Browse files
committed
Implement /batch method for sending/testing/bulk emails
1 parent 7841c8c commit 25d33e7

File tree

23 files changed

+663
-44
lines changed

23 files changed

+663
-44
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.mailtrap.examples.bulk;
2+
3+
import io.mailtrap.config.MailtrapConfig;
4+
import io.mailtrap.factory.MailtrapClientFactory;
5+
import io.mailtrap.model.request.emails.Address;
6+
import io.mailtrap.model.request.emails.MailtrapBatchMail;
7+
import io.mailtrap.model.request.emails.BatchEmailBase;
8+
import io.mailtrap.model.request.emails.MailtrapMail;
9+
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
public class Bulk {
14+
15+
private static final String TOKEN = "<YOUR MAILTRAP TOKEN>";
16+
private static final String SENDER_EMAIL = "[email protected]";
17+
private static final String RECIPIENT_EMAIL = "[email protected]";
18+
19+
public static void main(String[] args) {
20+
final var config = new MailtrapConfig.Builder()
21+
.token(TOKEN)
22+
.build();
23+
24+
final var client = MailtrapClientFactory.createMailtrapClient(config);
25+
26+
final var mail = MailtrapMail.builder()
27+
.from(new Address(SENDER_EMAIL))
28+
.to(List.of(new Address(RECIPIENT_EMAIL)))
29+
.subject("Hello from Mailtrap!")
30+
.text("Welcome to Mailtrap Bulk Sending!")
31+
.build();
32+
33+
final var batchMail = MailtrapBatchMail.builder()
34+
// Optionally you can add this `base` object - if you have some common data across emails
35+
// Each property can be overridden in `requests` for individual emails
36+
.base(BatchEmailBase.builder().subject("Base Subject for all emails").build())
37+
.requests(List.of(mail))
38+
.build();
39+
40+
System.out.println(client.bulkSendingApi().emails().batchSend(batchMail));
41+
}
42+
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.mailtrap.examples.sending;
2+
3+
import io.mailtrap.config.MailtrapConfig;
4+
import io.mailtrap.factory.MailtrapClientFactory;
5+
import io.mailtrap.model.request.emails.Address;
6+
import io.mailtrap.model.request.emails.MailtrapBatchMail;
7+
import io.mailtrap.model.request.emails.BatchEmailBase;
8+
import io.mailtrap.model.request.emails.MailtrapMail;
9+
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
public class Bulk {
14+
15+
private static final String TOKEN = "<YOUR MAILTRAP TOKEN>";
16+
private static final String SENDER_EMAIL = "[email protected]";
17+
private static final String RECIPIENT_EMAIL = "[email protected]";
18+
19+
public static void main(String[] args) {
20+
final var config = new MailtrapConfig.Builder()
21+
.token(TOKEN)
22+
.build();
23+
24+
final var client = MailtrapClientFactory.createMailtrapClient(config);
25+
26+
final var mail = MailtrapMail.builder()
27+
.from(new Address(SENDER_EMAIL))
28+
.to(List.of(new Address(RECIPIENT_EMAIL)))
29+
.subject("Hello from Mailtrap Sending!")
30+
.text("Welcome to Mailtrap Sending!")
31+
.build();
32+
33+
final var batchMail = MailtrapBatchMail.builder()
34+
// Optionally you can add this `base` object - if you have some common data across emails
35+
// Each property can be overridden in `requests` for individual emails
36+
.base(BatchEmailBase.builder().subject("Base Subject for all emails").build())
37+
.requests(List.of(mail))
38+
.build();
39+
40+
System.out.println(client.sendingApi().emails().batchSend(batchMail));
41+
}
42+
43+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.mailtrap.examples.testing;
2+
3+
import io.mailtrap.config.MailtrapConfig;
4+
import io.mailtrap.factory.MailtrapClientFactory;
5+
import io.mailtrap.model.request.emails.Address;
6+
import io.mailtrap.model.request.emails.MailtrapBatchMail;
7+
import io.mailtrap.model.request.emails.BatchEmailBase;
8+
import io.mailtrap.model.request.emails.MailtrapMail;
9+
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
public class Batch {
14+
15+
private static final String TOKEN = "<YOUR MAILTRAP TOKEN>";
16+
private static final String SENDER_EMAIL = "[email protected]";
17+
private static final String RECIPIENT_EMAIL = "[email protected]";
18+
private static final long INBOX_ID = 1337L;
19+
20+
public static void main(String[] args) {
21+
final var config = new MailtrapConfig.Builder()
22+
.token(TOKEN)
23+
.inboxId(INBOX_ID)
24+
.build();
25+
26+
final var client = MailtrapClientFactory.createMailtrapClient(config);
27+
28+
final var mail = MailtrapMail.builder()
29+
.from(new Address("John Doe", SENDER_EMAIL))
30+
.to(List.of(new Address("Jane Doe", RECIPIENT_EMAIL)))
31+
.templateUuid("813t39es-t74i-4308-b037-0n6bg8b1fe88")
32+
.templateVariables(Map.of(
33+
"user_name", "Jack Sparrow",
34+
"testing_template", "true"
35+
))
36+
.build();
37+
38+
final var batchMail = MailtrapBatchMail.builder()
39+
// Optionally you can add this `base` object - if you have some common data across emails
40+
// Each property can be overridden in `requests` for individual emails
41+
.base(BatchEmailBase.builder().subject("Base Subject for all emails").build())
42+
.requests(List.of(mail))
43+
.build();
44+
45+
System.out.println(client.testingApi().emails().batchSend(batchMail, config.getInboxId()));
46+
}
47+
48+
}

src/main/java/io/mailtrap/api/apiresource/SendApiResource.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.mailtrap.CustomValidator;
44
import io.mailtrap.config.MailtrapConfig;
55
import io.mailtrap.exception.InvalidRequestBodyException;
6+
import io.mailtrap.model.request.emails.MailtrapBatchMail;
67
import io.mailtrap.model.request.emails.MailtrapMail;
78
import org.apache.commons.collections4.MapUtils;
89
import org.apache.commons.lang3.StringUtils;
@@ -16,22 +17,28 @@ protected SendApiResource(MailtrapConfig config, CustomValidator customValidator
1617
super(config, customValidator);
1718
}
1819

20+
protected void assertBatchMailNotNull(MailtrapBatchMail batchMail){
21+
if (batchMail == null) {
22+
throw new InvalidRequestBodyException("BatchMail must not be null");
23+
}
24+
}
25+
1926
/**
2027
* Validates the request body of an email message and throws an exception if it is invalid.
2128
*
2229
* @param mail The email message to be validated.
2330
* @throws InvalidRequestBodyException If the request body is invalid.
2431
*/
25-
protected void validateRequestBodyOrThrowException(MailtrapMail mail) throws InvalidRequestBodyException {
32+
protected void validateMailPayload(MailtrapMail mail) throws InvalidRequestBodyException {
2633
// Check if the mail object itself is null
2734
if (mail == null) {
2835
throw new InvalidRequestBodyException("Mail must not be null");
2936
}
3037

3138
// Check if all three subject, text, and html are empty
3239
boolean isSubjectTextHtmlEmpty = StringUtils.isEmpty(mail.getSubject())
33-
&& StringUtils.isEmpty(mail.getText())
34-
&& StringUtils.isEmpty(mail.getHtml());
40+
&& StringUtils.isEmpty(mail.getText())
41+
&& StringUtils.isEmpty(mail.getHtml());
3542

3643
// Validate depending on whether the templateUuid is set
3744
if (StringUtils.isEmpty(mail.getTemplateUuid())) {

src/main/java/io/mailtrap/api/bulkemails/BulkEmails.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import io.mailtrap.exception.InvalidRequestBodyException;
44
import io.mailtrap.exception.http.HttpException;
5+
import io.mailtrap.model.request.emails.MailtrapBatchMail;
56
import io.mailtrap.model.request.emails.MailtrapMail;
7+
import io.mailtrap.model.response.emails.BatchSendResponse;
68
import io.mailtrap.model.response.emails.SendResponse;
79

810
/**
@@ -20,4 +22,6 @@ public interface BulkEmails {
2022
* @throws InvalidRequestBodyException If the request body is invalid.
2123
*/
2224
SendResponse send(MailtrapMail mail) throws HttpException, InvalidRequestBodyException;
25+
26+
BatchSendResponse batchSend(MailtrapBatchMail mail) throws HttpException, InvalidRequestBodyException;
2327
}

src/main/java/io/mailtrap/api/bulkemails/BulkEmailsImpl.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
import io.mailtrap.CustomValidator;
55
import io.mailtrap.api.apiresource.SendApiResource;
66
import io.mailtrap.config.MailtrapConfig;
7+
import io.mailtrap.exception.InvalidRequestBodyException;
8+
import io.mailtrap.exception.http.HttpException;
79
import io.mailtrap.http.RequestData;
10+
import io.mailtrap.model.request.emails.MailtrapBatchMail;
811
import io.mailtrap.model.request.emails.MailtrapMail;
12+
import io.mailtrap.model.response.emails.BatchSendResponse;
913
import io.mailtrap.model.response.emails.SendResponse;
1014

1115
/**
@@ -20,11 +24,24 @@ public BulkEmailsImpl(MailtrapConfig config, CustomValidator customValidator) {
2024

2125
@Override
2226
public SendResponse send(MailtrapMail mail) {
23-
validateRequestBodyOrThrowException(mail);
27+
validateMailPayload(mail);
2428
RequestData requestData = new RequestData();
2529
if (mail.getHeaders() != null) {
2630
requestData.setHeaders(mail.getHeaders());
2731
}
2832
return httpClient.post(apiHost + "/api/send", mail, requestData, SendResponse.class);
2933
}
34+
35+
@Override
36+
public BatchSendResponse batchSend(MailtrapBatchMail mail) throws HttpException, InvalidRequestBodyException {
37+
assertBatchMailNotNull(mail);
38+
39+
mail
40+
.getRequests()
41+
.forEach(this::validateMailPayload);
42+
43+
return
44+
httpClient.post(apiHost + "/api/batch", mail, new RequestData(), BatchSendResponse.class);
45+
46+
}
3047
}

src/main/java/io/mailtrap/api/sendingemails/SendingEmails.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import io.mailtrap.exception.InvalidRequestBodyException;
44
import io.mailtrap.exception.http.HttpException;
5+
import io.mailtrap.model.request.emails.MailtrapBatchMail;
56
import io.mailtrap.model.request.emails.MailtrapMail;
7+
import io.mailtrap.model.response.emails.BatchSendResponse;
68
import io.mailtrap.model.response.emails.SendResponse;
79

810
/**
@@ -20,4 +22,6 @@ public interface SendingEmails {
2022
* @throws InvalidRequestBodyException If the request body is invalid.
2123
*/
2224
SendResponse send(MailtrapMail mail) throws HttpException, InvalidRequestBodyException;
25+
26+
BatchSendResponse batchSend(MailtrapBatchMail mail) throws HttpException, InvalidRequestBodyException;
2327
}

src/main/java/io/mailtrap/api/sendingemails/SendingEmailsImpl.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
import io.mailtrap.CustomValidator;
55
import io.mailtrap.api.apiresource.SendApiResource;
66
import io.mailtrap.config.MailtrapConfig;
7+
import io.mailtrap.exception.InvalidRequestBodyException;
8+
import io.mailtrap.exception.http.HttpException;
79
import io.mailtrap.http.RequestData;
10+
import io.mailtrap.model.request.emails.MailtrapBatchMail;
811
import io.mailtrap.model.request.emails.MailtrapMail;
12+
import io.mailtrap.model.response.emails.BatchSendResponse;
913
import io.mailtrap.model.response.emails.SendResponse;
1014

1115
/**
@@ -20,12 +24,24 @@ public SendingEmailsImpl(MailtrapConfig config, CustomValidator customValidator)
2024

2125
@Override
2226
public SendResponse send(MailtrapMail mail) {
23-
validateRequestBodyOrThrowException(mail);
27+
validateMailPayload(mail);
2428
RequestData requestData = new RequestData();
2529
if (mail.getHeaders() != null) {
2630
requestData.setHeaders(mail.getHeaders());
2731
}
2832
return httpClient.post(apiHost + "/api/send", mail, requestData, SendResponse.class);
2933
}
3034

35+
@Override
36+
public BatchSendResponse batchSend(MailtrapBatchMail mail) throws HttpException, InvalidRequestBodyException {
37+
assertBatchMailNotNull(mail);
38+
39+
mail
40+
.getRequests()
41+
.forEach(this::validateMailPayload);
42+
43+
return
44+
httpClient.post(apiHost + "/api/batch", mail, new RequestData(), BatchSendResponse.class);
45+
}
46+
3147
}

src/main/java/io/mailtrap/api/testingemails/TestingEmails.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import io.mailtrap.exception.InvalidRequestBodyException;
44
import io.mailtrap.exception.http.HttpException;
5+
import io.mailtrap.model.request.emails.MailtrapBatchMail;
56
import io.mailtrap.model.request.emails.MailtrapMail;
7+
import io.mailtrap.model.response.emails.BatchSendResponse;
68
import io.mailtrap.model.response.emails.SendResponse;
79

810
/**
@@ -21,4 +23,6 @@ public interface TestingEmails {
2123
* @throws InvalidRequestBodyException If the request body is invalid.
2224
*/
2325
SendResponse send(MailtrapMail mail, long inboxId) throws HttpException, InvalidRequestBodyException;
26+
27+
BatchSendResponse batchSend(MailtrapBatchMail mail, long inboxId) throws HttpException, InvalidRequestBodyException;
2428
}

src/main/java/io/mailtrap/api/testingemails/TestingEmailsImpl.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
import io.mailtrap.CustomValidator;
55
import io.mailtrap.api.apiresource.SendApiResource;
66
import io.mailtrap.config.MailtrapConfig;
7+
import io.mailtrap.exception.InvalidRequestBodyException;
8+
import io.mailtrap.exception.http.HttpException;
79
import io.mailtrap.http.RequestData;
10+
import io.mailtrap.model.request.emails.MailtrapBatchMail;
811
import io.mailtrap.model.request.emails.MailtrapMail;
12+
import io.mailtrap.model.response.emails.BatchSendResponse;
913
import io.mailtrap.model.response.emails.SendResponse;
1014

1115
/**
@@ -20,11 +24,24 @@ public TestingEmailsImpl(MailtrapConfig config, CustomValidator customValidator)
2024

2125
@Override
2226
public SendResponse send(MailtrapMail mail, long inboxId) {
23-
validateRequestBodyOrThrowException(mail);
27+
validateMailPayload(mail);
2428
RequestData requestData = new RequestData();
2529
if (mail.getHeaders() != null) {
2630
requestData.setHeaders(mail.getHeaders());
2731
}
2832
return httpClient.post(String.format(apiHost + "/api/send/%d", inboxId), mail, requestData, SendResponse.class);
2933
}
34+
35+
@Override
36+
public BatchSendResponse batchSend(MailtrapBatchMail mail, long inboxId) throws HttpException, InvalidRequestBodyException {
37+
assertBatchMailNotNull(mail);
38+
39+
mail
40+
.getRequests()
41+
.forEach(this::validateMailPayload);
42+
43+
return
44+
httpClient.post(String.format(apiHost + "/api/batch/%d", inboxId), mail, new RequestData(), BatchSendResponse.class);
45+
46+
}
3047
}

0 commit comments

Comments
 (0)