15
15
*/
16
16
package io .awspring .cloud .samples .ses ;
17
17
18
+ import java .io .File ;
18
19
import org .springframework .boot .ApplicationRunner ;
19
20
import org .springframework .boot .SpringApplication ;
20
21
import org .springframework .boot .autoconfigure .SpringBootApplication ;
21
22
import org .springframework .context .annotation .Bean ;
23
+ import org .springframework .core .io .ClassPathResource ;
22
24
import org .springframework .mail .MailSender ;
23
25
import org .springframework .mail .SimpleMailMessage ;
26
+ import org .springframework .mail .javamail .JavaMailSender ;
27
+ import org .springframework .mail .javamail .MimeMessageHelper ;
24
28
import software .amazon .awssdk .services .ses .SesClient ;
25
29
import software .amazon .awssdk .services .ses .model .VerifyEmailAddressRequest ;
26
30
31
+ /**
32
+ * A sample application to demonstrate sending simple emails and emails with attachments.
33
+ * <p>
34
+ * To run this sample application, you need to do either of the following.
35
+ * <ul>
36
+ * <li>If you wish to send emails to a real email ID, you need to verify identities as mentioned in
37
+ * <a href="https://docs.aws.amazon.com/ses/latest/dg/creating-identities.html">the Amazon Simple Email Service
38
+ * docs.</a> After you do that, simply update this sample app with email IDs that you have verified with AWS.</li>
39
+ * <li>If you wish to just test out email sending capability in a test environment, you can do so by running localstack.
40
+ * Just issue the following command from the root of the `spring-cloud-aws-ses-sample`:
41
+ *
42
+ * <pre>
43
+ * docker-compose -f docker-compose.yml up -d
44
+ * </pre>
45
+ *
46
+ * See more information on localstack see <a href="https://docs.localstack.cloud/getting-started/">here</a> and
47
+ * <a href="https://docs.localstack.cloud/user-guide/aws/ses/">here</a>.</li>
48
+ * </ul>
49
+ * </p>
50
+ *
51
+ */
27
52
@ SpringBootApplication
28
53
public class MailSendingApplication {
29
54
private static final String SENDER =
"[email protected] " ;
@@ -37,7 +62,9 @@ public static void main(String[] args) {
37
62
ApplicationRunner applicationRunner (MailSender mailSender , SesClient sesClient ) {
38
63
return args -> {
39
64
sendAnEmail (mailSender , sesClient );
40
- // check localstack logs for sent email
65
+ sendAnEmailWithAttachment (mailSender , sesClient );
66
+ sendHtmlEmail (mailSender , sesClient );
67
+ // check localstack logs for sent email, if you use localstack for running this sample
41
68
};
42
69
}
43
70
@@ -56,4 +83,69 @@ public static void sendAnEmail(MailSender mailSender, SesClient sesClient) {
56
83
mailSender .send (simpleMailMessage );
57
84
}
58
85
86
+ /**
87
+ * To send emails with attachments, you must provide the Java Mail API and an implementation of the API in the
88
+ * classpath. See the dependencies provided in this sample app. If you don't provider an implementation of the Java
89
+ * Mail API, you would get the following exception at runtime.
90
+ *
91
+ * <pre>
92
+ * java.lang.IllegalStateException: Not provider of jakarta.mail.util.StreamProvider was found
93
+ * </pre>
94
+ *
95
+ * @param mailSender A {@link JavaMailSender}.
96
+ * @param sesClient An {@link SesClient}.
97
+ */
98
+ public static void sendAnEmailWithAttachment (MailSender mailSender , SesClient sesClient ) {
99
+ // e-mail address has to verified before we email it. If it is not verified SES will return error.
100
+ sesClient .verifyEmailAddress (VerifyEmailAddressRequest .builder ().emailAddress (RECIPIENT ).build ());
101
+ sesClient .verifyEmailAddress (VerifyEmailAddressRequest .builder ().emailAddress (SENDER ).build ());
102
+
103
+ // A JavaMailSender is needed. Spring Cloud AWS SES automatically configures a JavaMailSender when it finds
104
+ // the Java Mail API in the classpath. At runtime, an implementation of teh Java Mail API must also be
105
+ // available.
106
+ JavaMailSender javaMailSender = (JavaMailSender ) mailSender ;
107
+ javaMailSender .send (mimeMessage -> {
108
+ MimeMessageHelper helper = new MimeMessageHelper (mimeMessage , true , "UTF-8" );
109
+ helper .addTo (RECIPIENT );
110
+ helper .setFrom (SENDER );
111
+ File resource = new ClassPathResource ("answer.txt" ).getFile ();
112
+ helper .addAttachment ("answer.txt" , resource .getAbsoluteFile ());
113
+ helper .setSubject ("What is the meaning of life, the universe, and everything?" );
114
+ helper .setText ("Open the attached file for the answer you are seeking" , false );
115
+ });
116
+ }
117
+
118
+ /**
119
+ * To send HTML emails, you must provide the Java Mail API and an implementation of the API in the classpath. See
120
+ * the dependencies provided in this sample app. If you don't provider an implementation of the Java Mail API, you
121
+ * would get the following exception at runtime.
122
+ *
123
+ * <pre>
124
+ * java.lang.IllegalStateException: Not provider of jakarta.mail.util.StreamProvider was found
125
+ * </pre>
126
+ *
127
+ * @param mailSender A {@link JavaMailSender}.
128
+ * @param sesClient An {@link SesClient}.
129
+ */
130
+ public static void sendHtmlEmail (MailSender mailSender , SesClient sesClient ) {
131
+ // e-mail address has to verified before we email it. If it is not verified SES will return error.
132
+ sesClient .verifyEmailAddress (VerifyEmailAddressRequest .builder ().emailAddress (RECIPIENT ).build ());
133
+ sesClient .verifyEmailAddress (VerifyEmailAddressRequest .builder ().emailAddress (SENDER ).build ());
134
+
135
+ // A JavaMailSender is needed. Spring Cloud AWS SES automatically configures a JavaMailSender when it finds
136
+ // the Java Mail API in the classpath. At runtime, an implementation of the Java Mail API must also be
137
+ // available.
138
+ JavaMailSender javaMailSender = (JavaMailSender ) mailSender ;
139
+ javaMailSender .send (mimeMessage -> {
140
+ MimeMessageHelper helper = new MimeMessageHelper (mimeMessage , true , "UTF-8" );
141
+ helper .addTo (RECIPIENT );
142
+ helper .setFrom (SENDER );
143
+ helper .setSubject ("What is the meaning of life, the universe, and everything?" );
144
+ String htmlMessage = """
145
+ <h2>What is the meaning of life, the universe, and everything?</h2>
146
+ <h3>42</h3>
147
+ """ ;
148
+ helper .setText (htmlMessage , true );
149
+ });
150
+ }
59
151
}
0 commit comments