22
33import cloud .localstack .awssdkv1 .TestUtils ;
44
5+ import java .util .UUID ;
6+
57import com .amazonaws .services .simpleemail .AmazonSimpleEmailService ;
68import com .amazonaws .services .simpleemail .AmazonSimpleEmailServiceAsync ;
79import com .amazonaws .services .simpleemail .model .*;
@@ -25,26 +27,29 @@ public class SESMessagingTest {
2527 + "Amazon SES</a> using the <a href='https://aws.amazon.com/sdk-for-java/'>" + "AWS SDK for Java</a>" ;
2628 static final String TEXTBODY = "This email was sent through Amazon SES " + "using the AWS SDK for Java." ;
2729
28-
30+ private String templateName = "" ;
2931
3032 @ Test
3133 public void testSendEmail () throws Exception {
3234 AmazonSimpleEmailService client = TestUtils .getClientSES ();
3335
3436 VerifyEmailAddressRequest verifyEmailAddressRequest = new VerifyEmailAddressRequest ().withEmailAddress (TO );
3537 client .verifyEmailAddress (verifyEmailAddressRequest );
36-
38+
3739 verifyEmailAddressRequest .setEmailAddress (FROM );
3840 client .verifyEmailAddress (verifyEmailAddressRequest );
3941
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+
4048 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+
4853 SendEmailResult result = client .sendEmail (request );
4954 Assert .assertNotNull (result );
5055 }
@@ -55,19 +60,72 @@ public void testSendAsyncEmail() throws Exception {
5560
5661 VerifyEmailAddressRequest verifyEmailAddressRequest = new VerifyEmailAddressRequest ().withEmailAddress (TO );
5762 client .verifyEmailAddress (verifyEmailAddressRequest );
58-
63+
5964 verifyEmailAddressRequest .setEmailAddress (FROM );
6065 client .verifyEmailAddress (verifyEmailAddressRequest );
6166
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+
6273 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+
7078 SendEmailResult result = client .sendEmail (request );
7179 Assert .assertNotNull (result );
7280 }
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+ }
73131}
0 commit comments