From 4d4e6b43fb4352d887767ce4ca5732956104f595 Mon Sep 17 00:00:00 2001 From: hsombini Date: Sat, 7 Jan 2017 15:47:24 -0200 Subject: [PATCH] Email address field for email servers that does not use email as the auth username --- .../fr/xephi/authme/mail/SendMailSSL.java | 7 ++-- .../settings/properties/EmailSettings.java | 4 +++ .../fr/xephi/authme/mail/SendMailSSLTest.java | 32 +++++++++++++++++-- .../resources/fr/xephi/authme/config.test.yml | 2 ++ 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/main/java/fr/xephi/authme/mail/SendMailSSL.java b/src/main/java/fr/xephi/authme/mail/SendMailSSL.java index c90d9422b..a782d3a13 100644 --- a/src/main/java/fr/xephi/authme/mail/SendMailSSL.java +++ b/src/main/java/fr/xephi/authme/mail/SendMailSSL.java @@ -125,7 +125,10 @@ public class SendMailSSL { @VisibleForTesting HtmlEmail initializeMail(String emailAddress) throws EmailException { - String senderMail = settings.getProperty(EmailSettings.MAIL_ACCOUNT); + String senderMail = StringUtils.isEmpty(settings.getProperty(EmailSettings.MAIL_ADDRESS)) + ? settings.getProperty(EmailSettings.MAIL_ACCOUNT) + : settings.getProperty(EmailSettings.MAIL_ADDRESS); + String senderName = StringUtils.isEmpty(settings.getProperty(EmailSettings.MAIL_SENDER_NAME)) ? senderMail : settings.getProperty(EmailSettings.MAIL_SENDER_NAME); @@ -139,7 +142,7 @@ public class SendMailSSL { email.addTo(emailAddress); email.setFrom(senderMail, senderName); email.setSubject(settings.getProperty(EmailSettings.RECOVERY_MAIL_SUBJECT)); - email.setAuthentication(senderMail, mailPassword); + email.setAuthentication(settings.getProperty(EmailSettings.MAIL_ACCOUNT), mailPassword); setPropertiesForPort(email, port); return email; diff --git a/src/main/java/fr/xephi/authme/settings/properties/EmailSettings.java b/src/main/java/fr/xephi/authme/settings/properties/EmailSettings.java index fc5e9fd85..561ce511b 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/EmailSettings.java +++ b/src/main/java/fr/xephi/authme/settings/properties/EmailSettings.java @@ -27,6 +27,10 @@ public class EmailSettings implements SettingsHolder { public static final Property MAIL_PASSWORD = newProperty("Email.mailPassword", ""); + @Comment("Email address, fill when mailAccount is not the email address of the account") + public static final Property MAIL_ADDRESS = + newProperty("Email.mailAddress", ""); + @Comment("Custom sender name, replacing the mailAccount name in the email") public static final Property MAIL_SENDER_NAME = newProperty("Email.mailSenderName", ""); diff --git a/src/test/java/fr/xephi/authme/mail/SendMailSSLTest.java b/src/test/java/fr/xephi/authme/mail/SendMailSSLTest.java index 7d3746ea6..027aea57d 100644 --- a/src/test/java/fr/xephi/authme/mail/SendMailSSLTest.java +++ b/src/test/java/fr/xephi/authme/mail/SendMailSSLTest.java @@ -200,8 +200,8 @@ public class SendMailSSLTest { given(settings.getProperty(EmailSettings.SMTP_PORT)).willReturn(465); String smtpHost = "mail.example.com"; given(settings.getProperty(EmailSettings.SMTP_HOST)).willReturn(smtpHost); - String senderMail = "sender@example.org"; - given(settings.getProperty(EmailSettings.MAIL_ACCOUNT)).willReturn(senderMail); + String senderAccount = "sender@example.org"; + given(settings.getProperty(EmailSettings.MAIL_ACCOUNT)).willReturn(senderAccount); String senderName = "Server administration"; given(settings.getProperty(EmailSettings.MAIL_SENDER_NAME)).willReturn(senderName); @@ -212,7 +212,33 @@ public class SendMailSSLTest { assertThat(email, not(nullValue())); assertThat(email.getToAddresses(), hasSize(1)); assertThat(email.getToAddresses().get(0).getAddress(), equalTo("recipient@example.com")); - assertThat(email.getFromAddress().getAddress(), equalTo(senderMail)); + assertThat(email.getFromAddress().getAddress(), equalTo(senderAccount)); + assertThat(email.getFromAddress().getPersonal(), equalTo(senderName)); + assertThat(email.getHostName(), equalTo(smtpHost)); + assertThat(email.getSmtpPort(), equalTo("465")); + } + + @Test + public void shouldCreateEmailObjectWithAddress() throws EmailException { + // given + given(settings.getProperty(EmailSettings.SMTP_PORT)).willReturn(465); + String smtpHost = "mail.example.com"; + given(settings.getProperty(EmailSettings.SMTP_HOST)).willReturn(smtpHost); + String senderAccount = "exampleAccount"; + given(settings.getProperty(EmailSettings.MAIL_ACCOUNT)).willReturn(senderAccount); + String senderAddress = "mail@example.com"; + given(settings.getProperty(EmailSettings.MAIL_ADDRESS)).willReturn(senderAddress); + String senderName = "Server administration"; + given(settings.getProperty(EmailSettings.MAIL_SENDER_NAME)).willReturn(senderName); + + // when + HtmlEmail email = sendMailSSL.initializeMail("recipient@example.com"); + + // then + assertThat(email, not(nullValue())); + assertThat(email.getToAddresses(), hasSize(1)); + assertThat(email.getToAddresses().get(0).getAddress(), equalTo("recipient@example.com")); + assertThat(email.getFromAddress().getAddress(), equalTo(senderAddress)); assertThat(email.getFromAddress().getPersonal(), equalTo(senderName)); assertThat(email.getHostName(), equalTo(smtpHost)); assertThat(email.getSmtpPort(), equalTo("465")); diff --git a/src/test/resources/fr/xephi/authme/config.test.yml b/src/test/resources/fr/xephi/authme/config.test.yml index 37cc50b90..b68116387 100644 --- a/src/test/resources/fr/xephi/authme/config.test.yml +++ b/src/test/resources/fr/xephi/authme/config.test.yml @@ -318,6 +318,8 @@ Email: mailAccount: '' # Email account password mailPassword: '' + # Email address, fill when mailAccount is not email address of the account + mailAddress: '' # Custom SenderName, that replace the mailAccount name in the email mailSenderName: '' # Random password length