1
1
/*
2
- * Copyright 2012-2023 the original author or authors.
2
+ * Copyright 2012-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -68,11 +68,11 @@ private String getSubject(Message message) {
68
68
return message .getSubject ();
69
69
}
70
70
catch (MessagingException ex ) {
71
- throw new RuntimeException (ex );
71
+ throw new RuntimeException ("Failed to get message subject" , ex );
72
72
}
73
73
}
74
74
75
- private void assertMessagesContainSubject (Session session , String subject ) {
75
+ private void assertMessagesContainSubject (Session session , String subject ) throws MessagingException {
76
76
try (Store store = session .getStore ("pop3" )) {
77
77
String host = session .getProperty ("mail.pop3.host" );
78
78
int port = Integer .parseInt (session .getProperty ("mail.pop3.port" ));
@@ -86,27 +86,23 @@ private void assertMessagesContainSubject(Session session, String subject) {
86
86
.contains (subject ));
87
87
}
88
88
}
89
- catch (MessagingException ex ) {
90
- throw new RuntimeException (ex );
91
- }
92
89
}
93
90
94
91
@ Nested
95
92
class ImplicitTlsTests {
96
93
97
- final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
98
- .withConfiguration (AutoConfigurations .of (MailSenderAutoConfiguration .class , SslAutoConfiguration .class ));
99
-
100
94
@ Container
101
95
private static final MailpitContainer mailpit = TestImage .container (MailpitContainer .class )
102
- // force ssl connection
103
96
.withSmtpRequireTls (true )
104
97
.withSmtpTlsCert (MountableFile
105
98
.forClasspathResource ("/org/springframework/boot/autoconfigure/mail/ssl/test-server.crt" ))
106
99
.withSmtpTlsKey (MountableFile
107
100
.forClasspathResource ("/org/springframework/boot/autoconfigure/mail/ssl/test-server.key" ))
108
101
.withPop3Auth ("user:pass" );
109
102
103
+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
104
+ .withConfiguration (AutoConfigurations .of (MailSenderAutoConfiguration .class , SslAutoConfiguration .class ));
105
+
110
106
@ Test
111
107
void sendEmailWithSslEnabledAndCert () {
112
108
this .contextRunner .withPropertyValues ("spring.mail.host:" + mailpit .getHost (),
@@ -115,14 +111,11 @@ void sendEmailWithSslEnabledAndCert() {
115
111
"spring.ssl.bundle.pem.test-bundle.truststore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-ca.crt" ,
116
112
"spring.ssl.bundle.pem.test-bundle.keystore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.crt" ,
117
113
"spring.ssl.bundle.pem.test-bundle.keystore.private-key=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.key" ,
118
- // pop3
119
114
"spring.mail.properties.mail.pop3.host:" + mailpit .getHost (),
120
115
"spring.mail.properties.mail.pop3.port:" + mailpit .getPop3Port ())
121
116
.run ((context ) -> {
122
117
JavaMailSenderImpl mailSender = context .getBean (JavaMailSenderImpl .class );
123
-
124
118
mailSender .send (createMessage ("Hello World!" ));
125
-
126
119
assertMessagesContainSubject (mailSender .getSession (), "Hello World!" );
127
120
});
128
121
}
@@ -134,7 +127,6 @@ void sendEmailWithSslEnabledWithoutCert() {
134
127
"spring.mail.port:" + mailpit .getSmtpPort (), "spring.mail.ssl.enabled:true" )
135
128
.run ((context ) -> {
136
129
JavaMailSenderImpl mailSender = context .getBean (JavaMailSenderImpl .class );
137
-
138
130
assertThatException ().isThrownBy (() -> mailSender .send (createMessage ("Should fail" )))
139
131
.withRootCauseInstanceOf (CertPathBuilderException .class );
140
132
});
@@ -150,7 +142,6 @@ void sendEmailWithoutSslWithCert() {
150
142
"spring.ssl.bundle.pem.test-bundle.keystore.private-key=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.key" )
151
143
.run ((context ) -> {
152
144
JavaMailSenderImpl mailSender = context .getBean (JavaMailSenderImpl .class );
153
-
154
145
assertThatException ().isThrownBy (() -> mailSender .send (createMessage ("Should fail" )))
155
146
.withRootCauseInstanceOf (SocketTimeoutException .class );
156
147
});
@@ -161,9 +152,6 @@ void sendEmailWithoutSslWithCert() {
161
152
@ Nested
162
153
class StarttlsTests {
163
154
164
- final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
165
- .withConfiguration (AutoConfigurations .of (MailSenderAutoConfiguration .class , SslAutoConfiguration .class ));
166
-
167
155
@ Container
168
156
private static final MailpitContainer mailpit = TestImage .container (MailpitContainer .class )
169
157
.withSmtpRequireStarttls (true )
@@ -173,6 +161,9 @@ class StarttlsTests {
173
161
.forClasspathResource ("/org/springframework/boot/autoconfigure/mail/ssl/test-server.key" ))
174
162
.withPop3Auth ("user:pass" );
175
163
164
+ final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
165
+ .withConfiguration (AutoConfigurations .of (MailSenderAutoConfiguration .class , SslAutoConfiguration .class ));
166
+
176
167
@ Test
177
168
void sendEmailWithStarttlsAndCertAndSslDisabled () {
178
169
this .contextRunner .withPropertyValues ("spring.mail.host:" + mailpit .getHost (),
@@ -182,14 +173,11 @@ void sendEmailWithStarttlsAndCertAndSslDisabled() {
182
173
"spring.ssl.bundle.pem.test-bundle.truststore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-ca.crt" ,
183
174
"spring.ssl.bundle.pem.test-bundle.keystore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.crt" ,
184
175
"spring.ssl.bundle.pem.test-bundle.keystore.private-key=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.key" ,
185
- // pop3
186
176
"spring.mail.properties.mail.pop3.host:" + mailpit .getHost (),
187
177
"spring.mail.properties.mail.pop3.port:" + mailpit .getPop3Port ())
188
178
.run ((context ) -> {
189
179
JavaMailSenderImpl mailSender = context .getBean (JavaMailSenderImpl .class );
190
-
191
180
mailSender .send (createMessage ("Sent with STARTTLS" ));
192
-
193
181
assertMessagesContainSubject (mailSender .getSession (), "Sent with STARTTLS" );
194
182
});
195
183
}
@@ -203,12 +191,10 @@ void sendEmailWithStarttlsAndCertAndSslEnabled() {
203
191
"spring.ssl.bundle.pem.test-bundle.truststore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-ca.crt" ,
204
192
"spring.ssl.bundle.pem.test-bundle.keystore.certificate=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.crt" ,
205
193
"spring.ssl.bundle.pem.test-bundle.keystore.private-key=classpath:org/springframework/boot/autoconfigure/mail/ssl/test-client.key" ,
206
- // pop3
207
194
"spring.mail.properties.mail.pop3.host:" + mailpit .getHost (),
208
195
"spring.mail.properties.mail.pop3.port:" + mailpit .getPop3Port ())
209
196
.run ((context ) -> {
210
197
JavaMailSenderImpl mailSender = context .getBean (JavaMailSenderImpl .class );
211
-
212
198
assertThatException ().isThrownBy (() -> mailSender .send (createMessage ("Should fail" )))
213
199
.withRootCauseInstanceOf (SSLException .class );
214
200
});
@@ -223,7 +209,6 @@ void sendEmailWithStarttlsWithoutCert() {
223
209
"spring.mail.properties.mail.smtp.starttls.required:true" )
224
210
.run ((context ) -> {
225
211
JavaMailSenderImpl mailSender = context .getBean (JavaMailSenderImpl .class );
226
-
227
212
assertThatException ().isThrownBy (() -> mailSender .send (createMessage ("Should fail" )))
228
213
.withRootCauseInstanceOf (CertPathBuilderException .class );
229
214
});
0 commit comments