2
2
3
3
import cloud .localstack .awssdkv1 .TestUtils ;
4
4
5
+ import java .util .UUID ;
6
+
5
7
import com .amazonaws .services .simpleemail .AmazonSimpleEmailService ;
6
8
import com .amazonaws .services .simpleemail .AmazonSimpleEmailServiceAsync ;
7
9
import com .amazonaws .services .simpleemail .model .*;
@@ -25,26 +27,29 @@ public class SESMessagingTest {
25
27
+ "Amazon SES</a> using the <a href='https://aws.amazon.com/sdk-for-java/'>" + "AWS SDK for Java</a>" ;
26
28
static final String TEXTBODY = "This email was sent through Amazon SES " + "using the AWS SDK for Java." ;
27
29
28
-
30
+ private String templateName = "" ;
29
31
30
32
@ Test
31
33
public void testSendEmail () throws Exception {
32
34
AmazonSimpleEmailService client = TestUtils .getClientSES ();
33
35
34
36
VerifyEmailAddressRequest verifyEmailAddressRequest = new VerifyEmailAddressRequest ().withEmailAddress (TO );
35
37
client .verifyEmailAddress (verifyEmailAddressRequest );
36
-
38
+
37
39
verifyEmailAddressRequest .setEmailAddress (FROM );
38
40
client .verifyEmailAddress (verifyEmailAddressRequest );
39
41
42
+ Message message = new Message ()
43
+ .withBody (new Body ()
44
+ .withHtml (new Content ().withCharset ("UTF-8" ).withData (HTMLBODY ))
45
+ .withText (new Content ().withCharset ("UTF-8" ).withData (TEXTBODY )))
46
+ .withSubject (new Content ().withCharset ("UTF-8" ).withData (SUBJECT ));
47
+
40
48
SendEmailRequest request = new SendEmailRequest ()
41
- .withDestination (new Destination ().withToAddresses (TO ))
42
- .withMessage (new Message ()
43
- .withBody (new Body ().withHtml (new Content ().withCharset ("UTF-8" ).withData (HTMLBODY ))
44
- .withText (new Content ().withCharset ("UTF-8" ).withData (TEXTBODY )))
45
- .withSubject (new Content ().withCharset ("UTF-8" ).withData (SUBJECT )))
46
- .withSource (FROM ).withConfigurationSetName (CONFIGSET );
47
-
49
+ .withSource (FROM ).withConfigurationSetName (CONFIGSET )
50
+ .withDestination (new Destination ().withToAddresses (TO ))
51
+ .withMessage (message );
52
+
48
53
SendEmailResult result = client .sendEmail (request );
49
54
Assert .assertNotNull (result );
50
55
}
@@ -55,19 +60,72 @@ public void testSendAsyncEmail() throws Exception {
55
60
56
61
VerifyEmailAddressRequest verifyEmailAddressRequest = new VerifyEmailAddressRequest ().withEmailAddress (TO );
57
62
client .verifyEmailAddress (verifyEmailAddressRequest );
58
-
63
+
59
64
verifyEmailAddressRequest .setEmailAddress (FROM );
60
65
client .verifyEmailAddress (verifyEmailAddressRequest );
61
66
67
+ Message message = new Message ()
68
+ .withBody (new Body ()
69
+ .withHtml (new Content ().withCharset ("UTF-8" ).withData (HTMLBODY ))
70
+ .withText (new Content ().withCharset ("UTF-8" ).withData (TEXTBODY )))
71
+ .withSubject (new Content ().withCharset ("UTF-8" ).withData (SUBJECT ));
72
+
62
73
SendEmailRequest request = new SendEmailRequest ()
63
- .withDestination (new Destination ().withToAddresses (TO ))
64
- .withMessage (new Message ()
65
- .withBody (new Body ().withHtml (new Content ().withCharset ("UTF-8" ).withData (HTMLBODY ))
66
- .withText (new Content ().withCharset ("UTF-8" ).withData (TEXTBODY )))
67
- .withSubject (new Content ().withCharset ("UTF-8" ).withData (SUBJECT )))
68
- .withSource (FROM ).withConfigurationSetName (CONFIGSET );
69
-
74
+ .withSource (FROM ).withConfigurationSetName (CONFIGSET )
75
+ .withDestination (new Destination ().withToAddresses (TO ))
76
+ .withMessage (message );
77
+
70
78
SendEmailResult result = client .sendEmail (request );
71
79
Assert .assertNotNull (result );
72
80
}
81
+
82
+ @ Test
83
+ public void testCreateTemplate () throws Exception {
84
+ AmazonSimpleEmailService client = TestUtils .getClientSES ();
85
+
86
+ templateName = "test-s-" + UUID .randomUUID ().toString ();
87
+ String subjectPart = "Greetings, {{name}}!" ;
88
+ String htmlPart = "<h1>Hello {{name}},</h1><p>Your favorite animal is {{favoriteanimal}}.</p>" ;
89
+ String textPart = "Dear {{name}},\r \n Your favorite animal is {{favoriteanimal}}." ;
90
+
91
+ Template template = new Template ()
92
+ .withHtmlPart (htmlPart )
93
+ .withTextPart (textPart )
94
+ .withSubjectPart (subjectPart )
95
+ .withTemplateName (templateName );
96
+
97
+ CreateTemplateRequest request = new CreateTemplateRequest ().withTemplate (template );
98
+ CreateTemplateResult result = client .createTemplate (request );
99
+
100
+ Assert .assertNotNull (result );
101
+ }
102
+
103
+ @ Test
104
+ public void testSendTemplatedEmail () throws Throwable {
105
+ AmazonSimpleEmailService client = TestUtils .getClientSES ();
106
+
107
+ VerifyEmailAddressRequest verifyEmailAddressRequest = new VerifyEmailAddressRequest ().withEmailAddress (TO );
108
+ client .verifyEmailAddress (verifyEmailAddressRequest );
109
+
110
+ verifyEmailAddressRequest .setEmailAddress (FROM );
111
+ client .verifyEmailAddress (verifyEmailAddressRequest );
112
+
113
+ try {
114
+ this .testCreateTemplate ();
115
+ } catch (Exception e ) {
116
+ throw new Throwable ("Error creating template to send" );
117
+ }
118
+
119
+ String templateData = "{ \" name\" :\" Alejandro\" , \" favoriteanimal\" : \" alligator\" }" ;
120
+
121
+ SendTemplatedEmailRequest request = new SendTemplatedEmailRequest ()
122
+ .withConfigurationSetName (CONFIGSET )
123
+ .withSource (FROM )
124
+ .withDestination (new Destination ().withToAddresses (TO ))
125
+ .withTemplate (templateName )
126
+ .withTemplateData (templateData );
127
+
128
+ SendTemplatedEmailResult result = client .sendTemplatedEmail (request );
129
+ Assert .assertNotNull (result );
130
+ }
73
131
}
0 commit comments