Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,15 @@ public void testIsEmailAddressValid() {
assertFalse(mailHelper.isEmailAddressValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@example.com"));
}

@Test
public void testIsEmailAddressValidAsciiOnly() {
MailHelper mailHelper = BEANS.get(MailHelper.class);
assertFalse(mailHelper.isEmailAddressValid("foo@bär.de", false));
assertFalse(mailHelper.isEmailAddressValid("foo@domaintest.みんな", false));
assertFalse(mailHelper.isEmailAddressValid("füü@bär.de", false));
assertFalse(mailHelper.isEmailAddressValid("I❤\uFE0FCHOCOLATE@example.com", false));
}

@Test
public void testEnsureFromAddressWithoutFromAndDefaultFrom() throws MessagingException {
MailMessage mailMessage = BEANS.get(MailMessage.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,13 +955,17 @@ protected String getPartCharsetInternal(Part part) throws MessagingException {
*/
@SuppressWarnings("squid:S1166")
public boolean isEmailAddressValid(String emailAddress) {
return isEmailAddressValid(emailAddress, true);
}

public boolean isEmailAddressValid(String emailAddress, boolean utf8) {
if (StringUtility.isNullOrEmpty(emailAddress)) {
return false;
}

try {
new InternetAddress(BEANS.get(MailIDNConverter.class).toASCII(emailAddress), true);
return true;
return utf8 || StandardCharsets.US_ASCII.newEncoder().canEncode(emailAddress);
}
catch (AddressException | IllegalArgumentException e) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,13 @@ protected Session createSession(SmtpServerConfig config) {
if (readTimeout != null) {
props.setProperty(propertyBaseName + ".timeout", Integer.toString(readTimeout));
}
if (config.isUseUtf8()) {
props.setProperty("mail.mime.allowutf8", "true");
}

if (!CollectionUtility.isEmpty(config.getAdditionalSessionProperties())) {
props.putAll(config.getAdditionalSessionProperties());
}

LOG.debug("Session created with properties {}", props);

return Session.getInstance(props, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class SmtpServerConfig {
private boolean m_useStartTls;
private String m_sslProtocols;
private boolean m_oAuth2;
private boolean m_useUtf8;

private Map<String, String> m_additionalSessionProperties;

Expand Down Expand Up @@ -209,6 +210,19 @@ public SmtpServerConfig withMaxMessagesPerConnection(int maxMessagesPerConnectio
return this;
}

public boolean isUseUtf8() {
return m_useUtf8;
}

/**
* @param useUtf8
* Enables UTF-8 support.
*/
public SmtpServerConfig withUseUtf8(boolean useUtf8) {
m_useUtf8 = useUtf8;
return this;
}

@Override
public int hashCode() {
final int prime = 31;
Expand All @@ -225,6 +239,7 @@ public int hashCode() {
result = prime * result + (m_useSmtps ? 1231 : 1237);
result = prime * result + (m_useStartTls ? 1231 : 1237);
result = prime * result + ((m_username == null) ? 0 : m_username.hashCode());
result = prime * result + (m_useUtf8 ? 1231 : 1237);
return result;
}

Expand Down Expand Up @@ -306,6 +321,9 @@ else if (!m_sslProtocols.equals(other.m_sslProtocols)) {
else if (!m_username.equals(other.m_username)) {
return false;
}
if (m_useUtf8 != other.m_useUtf8) {
return false;
}
return true;
}
}
Loading