Skip to content

Commit 2f39d78

Browse files
committed
#429: mail.<protocol>.username -> mail.<protocol>.user, as per Sun/Jakarta documentation. This is the default user property in case manual authenticator is not used (which is the case with Simple Java Mail if a password was not provided by the user)
1 parent 6d64a39 commit 2f39d78

File tree

5 files changed

+60
-34
lines changed

5 files changed

+60
-34
lines changed

modules/core-module/src/main/java/org/simplejavamail/api/mailer/config/TransportStrategy.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ public String propertyNamePort() {
9797
}
9898

9999
/**
100-
* @return "mail.smtp.username"
100+
* @return "mail.smtp.user"
101101
*/
102102
@Override
103103
public String propertyNameUsername() {
104-
return "mail.smtp.username";
104+
return "mail.smtp.user";
105105
}
106106

107107
/**
@@ -258,11 +258,11 @@ public String propertyNamePort() {
258258
}
259259

260260
/**
261-
* @return "mail.smtps.username"
261+
* @return "mail.smtps.user"
262262
*/
263263
@Override
264264
public String propertyNameUsername() {
265-
return "mail.smtps.username";
265+
return "mail.smtps.user";
266266
}
267267

268268
/**
@@ -397,11 +397,11 @@ public String propertyNamePort() {
397397
}
398398

399399
/**
400-
* @return "mail.smtp.username"
400+
* @return "mail.smtp.user"
401401
*/
402402
@Override
403403
public String propertyNameUsername() {
404-
return "mail.smtp.username";
404+
return "mail.smtp.user";
405405
}
406406

407407
/**

modules/simple-java-mail/src/test/java/org/simplejavamail/mailer/MailerLiveTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,19 @@ public class MailerLiveTest {
6161
private static final String RESOURCES_PKCS = determineResourceFolder("simple-java-mail") + "/test/resources/pkcs12";
6262

6363
private static final Integer SERVER_PORT = 251;
64+
65+
private static final String USERNAME = "usey";
66+
private static final String PASSWORD = "passy";
6467

6568
@Rule
66-
public final SmtpServerRule smtpServerRule = new SmtpServerRule(SERVER_PORT);
69+
public final SmtpServerRule smtpServerRule = new SmtpServerRule(SERVER_PORT, "usey", "passy");
6770

6871
private Mailer mailer;
6972

7073
@Before
7174
public void setup() {
7275
ConfigLoaderTestHelper.clearConfigProperties();
73-
mailer = MailerBuilder.withSMTPServer("localhost", SERVER_PORT).buildMailer();
76+
mailer = MailerBuilder.withSMTPServer("localhost", SERVER_PORT, USERNAME, PASSWORD).buildMailer();
7477
}
7578

7679
@Test
@@ -141,7 +144,7 @@ public void createMailSession_OutlookMessageDefaultSmimeSignTest()
141144
throws IOException, MessagingException, ExecutionException, InterruptedException {
142145
// override the default from the @Before test
143146
mailer = MailerBuilder
144-
.withSMTPServer("localhost", SERVER_PORT)
147+
.withSMTPServer("localhost", SERVER_PORT, USERNAME, PASSWORD)
145148
.signByDefaultWithSmime(new File(RESOURCES_PKCS + "/smime_keystore.pkcs12"), "letmein", "smime_test_user_alias", "letmein")
146149
.buildMailer();
147150

modules/simple-java-mail/src/test/java/org/simplejavamail/mailer/MailerSOCKSLiveTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class MailerSOCKSLiveTest {
2121
private static final Integer PROXY_SERVER_PORT = 253;
2222

2323
@Rule
24-
public final SmtpServerRule smtpServerRule = new SmtpServerRule(SMTP_SERVER_PORT);
24+
public final SmtpServerRule smtpServerRule = new SmtpServerRule(SMTP_SERVER_PORT, null, null);
2525
@ClassRule
2626
public static final SockServerRule sockServerRule = new SockServerRule(PROXY_SERVER_PORT);
2727

modules/simple-java-mail/src/test/java/org/simplejavamail/mailer/MailerTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void createMailSession_MinimalConstructor_WithoutConfig() {
8787
assertThat(session.getProperty("mail.smtp.ssl.trust")).isEqualTo("*");
8888
assertThat(session.getProperty("mail.smtp.ssl.checkserveridentity")).isNull();
8989

90-
assertThat(session.getProperty("mail.smtp.username")).isNull();
90+
assertThat(session.getProperty("mail.smtp.user")).isNull();
9191
assertThat(session.getProperty("mail.smtp.auth")).isNull();
9292
assertThat(session.getProperty("mail.smtp.socks.host")).isNull();
9393
assertThat(session.getProperty("mail.smtp.socks.port")).isNull();
@@ -125,7 +125,7 @@ public void createMailSession_AnonymousProxyConstructor_WithoutConfig() {
125125
assertThat(session.getProperty("mail.smtp.starttls.required")).isEqualTo("true");
126126
assertThat(session.getProperty("mail.smtp.ssl.checkserveridentity")).isEqualTo("true");
127127

128-
assertThat(session.getProperty("mail.smtp.username")).isEqualTo("username smtp");
128+
assertThat(session.getProperty("mail.smtp.user")).isEqualTo("username smtp");
129129
assertThat(session.getProperty("mail.smtp.auth")).isEqualTo("true");
130130
assertThat(session.getProperty("mail.smtp.socks.host")).isEqualTo("proxy host");
131131
assertThat(session.getProperty("mail.smtp.socks.port")).isEqualTo("1080");
@@ -150,7 +150,7 @@ public void createMailSession_MaximumConstructor_WithoutConfig() {
150150
assertThat(session.getProperty("mail.smtp.starttls.enable")).isEqualTo("true");
151151
assertThat(session.getProperty("mail.smtp.starttls.required")).isEqualTo("true");
152152
assertThat(session.getProperty("mail.smtp.ssl.checkserveridentity")).isEqualTo("true");
153-
assertThat(session.getProperty("mail.smtp.username")).isEqualTo("username smtp");
153+
assertThat(session.getProperty("mail.smtp.user")).isEqualTo("username smtp");
154154
assertThat(session.getProperty("mail.smtp.auth")).isEqualTo("true");
155155
// the following two are because authentication is needed, otherwise proxy would be straightworward
156156
assertThat(session.getProperty("mail.smtp.socks.host")).isEqualTo("localhost");
@@ -176,7 +176,7 @@ public void createMailSession_MinimalConstructor_WithConfig() {
176176
assertThat(session.getProperty("mail.smtp.starttls.enable")).isEqualTo("true");
177177
assertThat(session.getProperty("mail.smtp.starttls.required")).isEqualTo("true");
178178
assertThat(session.getProperty("mail.smtp.ssl.checkserveridentity")).isEqualTo("true");
179-
assertThat(session.getProperty("mail.smtp.username")).isEqualTo("username smtp");
179+
assertThat(session.getProperty("mail.smtp.user")).isEqualTo("username smtp");
180180
assertThat(session.getProperty("mail.smtp.auth")).isEqualTo("true");
181181
// the following two are because authentication is needed, otherwise proxy would be straightworward
182182
assertThat(session.getProperty("mail.smtp.socks.host")).isEqualTo("localhost");
@@ -206,7 +206,7 @@ public void createMailSession_MinimalConstructor_WithConfig_OPPORTUNISTIC_TLS()
206206
assertThat(session.getProperty("mail.smtp.starttls.required")).isNull();
207207
assertThat(session.getProperty("mail.smtp.ssl.checkserveridentity")).isNull();
208208

209-
assertThat(session.getProperty("mail.smtp.username")).isEqualTo("username smtp");
209+
assertThat(session.getProperty("mail.smtp.user")).isEqualTo("username smtp");
210210
assertThat(session.getProperty("mail.smtp.auth")).isEqualTo("true");
211211
// the following two are because authentication is needed, otherwise proxy would be straightworward
212212
assertThat(session.getProperty("mail.smtp.socks.host")).isEqualTo("localhost");
@@ -236,7 +236,7 @@ public void createMailSession_MinimalConstructor_WithConfig_OPPORTUNISTIC_TLS_Ma
236236
assertThat(session.getProperty("mail.smtp.ssl.trust")).isEqualTo("*");
237237
assertThat(session.getProperty("mail.smtp.ssl.checkserveridentity")).isNull();
238238

239-
assertThat(session.getProperty("mail.smtp.username")).isEqualTo("username smtp");
239+
assertThat(session.getProperty("mail.smtp.user")).isEqualTo("username smtp");
240240
assertThat(session.getProperty("mail.smtp.auth")).isEqualTo("true");
241241
// the following two are because authentication is needed, otherwise proxy would be straightworward
242242
assertThat(session.getProperty("mail.smtp.socks.host")).isEqualTo("localhost");
@@ -256,7 +256,7 @@ public void createMailSession_MaximumConstructor_WithConfig() {
256256
assertThat(session.getProperty("mail.smtp.starttls.enable")).isEqualTo("true");
257257
assertThat(session.getProperty("mail.smtp.starttls.required")).isEqualTo("true");
258258
assertThat(session.getProperty("mail.smtp.ssl.checkserveridentity")).isEqualTo("true");
259-
assertThat(session.getProperty("mail.smtp.username")).isEqualTo("overridden username smtp");
259+
assertThat(session.getProperty("mail.smtp.user")).isEqualTo("overridden username smtp");
260260
assertThat(session.getProperty("mail.smtp.auth")).isEqualTo("true");
261261
// the following two are because authentication is needed, otherwise proxy would be straightworward
262262
assertThat(session.getProperty("mail.smtp.socks.host")).isEqualTo("localhost");
@@ -276,7 +276,7 @@ public void createMailSession_MaximumConstructor_WithConfig_TLS() {
276276
assertThat(session.getProperty("mail.smtps.port")).isEqualTo("25");
277277
assertThat(session.getProperty("mail.transport.protocol")).isEqualTo("smtps");
278278
assertThat(session.getProperty("mail.smtps.quitwait")).isEqualTo("false");
279-
assertThat(session.getProperty("mail.smtps.username")).isEqualTo("overridden username smtp");
279+
assertThat(session.getProperty("mail.smtps.user")).isEqualTo("overridden username smtp");
280280
assertThat(session.getProperty("mail.smtps.auth")).isEqualTo("true");
281281
assertThat(session.getProperty("extra1")).isEqualTo("overridden value1");
282282
assertThat(session.getProperty("extra2")).isEqualTo("overridden value2");

modules/simple-java-mail/src/test/java/testutil/testrules/SmtpServerRule.java

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
import jakarta.mail.MessagingException;
44
import jakarta.mail.internet.MimeMessage;
5+
import lombok.RequiredArgsConstructor;
56
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
68
import org.junit.rules.ExternalResource;
9+
import org.subethamail.smtp.MessageContext;
10+
import org.subethamail.smtp.auth.EasyAuthenticationHandlerFactory;
11+
import org.subethamail.smtp.auth.LoginFailedException;
12+
import org.subethamail.smtp.auth.UsernamePasswordValidator;
713
import org.subethamail.smtp.server.SMTPServer;
814
import org.subethamail.wiser.Wiser;
915
import org.subethamail.wiser.WiserMessage;
@@ -12,12 +18,13 @@
1218
import java.io.InputStream;
1319
import java.io.PrintStream;
1420
import java.util.List;
21+
import java.util.Objects;
1522

1623
import static java.lang.String.format;
1724
import static org.assertj.core.api.Assertions.assertThat;
1825

1926
/**
20-
* SmtpServerRule - a TestRule wrapping a Wiser instance (a SMTP server in Java), started and stopped right before and after each test.
27+
* SmtpServerRule - a TestRule wrapping a Wiser instance (an SMTP server in Java), started and stopped right before and after each test.
2128
* <br>
2229
* SmtpServerRule exposes the same methods as the {@link Wiser} instance by delegating the implementation to the instance. These methods, however, can not be
2330
* used outside a JUnit statement (otherwise an {@link IllegalStateException} is raised).
@@ -26,27 +33,43 @@
2633
*/
2734
public class SmtpServerRule extends ExternalResource {
2835
private final Wiser wiser;
29-
30-
public SmtpServerRule(@NotNull Integer port) {
31-
this.wiser = Wiser.accepter((from, recipient) -> true).port(port);
36+
37+
@RequiredArgsConstructor
38+
static class RequiredUsernamePasswordValidator implements UsernamePasswordValidator {
39+
@Nullable final String username;
40+
@Nullable final String password;
41+
42+
@Override
43+
public void login(String username, String password, MessageContext context) throws LoginFailedException {
44+
if (!Objects.equals(this.username, username) || !Objects.equals(this.password, password)) {
45+
throw new LoginFailedException();
46+
}
47+
}
3248
}
33-
49+
50+
public SmtpServerRule(@NotNull Integer port, @Nullable String username, @Nullable String password) {
51+
this.wiser = Wiser.create(SMTPServer
52+
.port(port)
53+
.authenticationHandlerFactory(new EasyAuthenticationHandlerFactory(new RequiredUsernamePasswordValidator(username, password)))
54+
.requireAuth(password != null));
55+
}
56+
3457
@Override
3558
protected void before() {
3659
this.wiser.start();
3760
}
38-
61+
3962
@Override
4063
protected void after() {
4164
this.wiser.stop();
4265
}
43-
66+
4467
@NotNull
4568
public Wiser getWiser() {
4669
checkState("getWiser()");
4770
return this.wiser;
4871
}
49-
72+
5073
@NotNull
5174
public List<WiserMessage> getMessages() {
5275
checkState("getMessages()");
@@ -79,39 +102,39 @@ public MimeMessage getMessage(String envelopeReceiver)
79102
throws MessagingException {
80103
checkState("getMessages()");
81104
List<WiserMessage> messages = getMessages();
82-
105+
83106
WiserMessage wiserMessage = messages.stream()
84-
.filter(m->m.getEnvelopeReceiver().equals(envelopeReceiver))
107+
.filter(m -> m.getEnvelopeReceiver().equals(envelopeReceiver))
85108
.findFirst()
86109
.orElseThrow(() -> new AssertionError("message not found for recipient " + envelopeReceiver));
87-
110+
88111
messages.remove(wiserMessage);
89112
return wiserMessage.getMimeMessage();
90113
}
91-
114+
92115
@NotNull
93116
public SMTPServer getServer() {
94117
checkState("getServer()");
95118
return wiser.getServer();
96119
}
97-
120+
98121
public boolean accept(String from, String recipient) {
99122
checkState("accept(String, String)");
100123
return wiser.accept(from, recipient);
101124
}
102-
125+
103126
public void deliver(String from, String recipient, InputStream data)
104127
throws IOException {
105128
checkState("deliver(String, String, InputStream)");
106129
wiser.deliver(from, recipient, data);
107130
}
108-
131+
109132
public void dumpMessages(PrintStream out)
110133
throws MessagingException {
111134
checkState("dumpMessages(PrintStream)");
112135
wiser.dumpMessages(out);
113136
}
114-
137+
115138
private void checkState(String method) {
116139
if (this.wiser == null) {
117140
throw new IllegalStateException(format("%s must not be called outside of a JUnit statement", method));

0 commit comments

Comments
 (0)