From fd8efc5620e84d319aeebc4d514f25bd62acf046 Mon Sep 17 00:00:00 2001 From: Felix Schumacher Date: Sat, 20 Feb 2021 11:03:02 +0100 Subject: [PATCH] Encode the personal part of email addresses in SMTP Sampler Often those personal parts contain umlauts. Try to find these and let MimeUtility encode those as quoted encodings. Bugzilla Id: 65149 --- .../protocol/smtp/sampler/SmtpSampler.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/protocol/mail/src/main/java/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java b/src/protocol/mail/src/main/java/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java index 70697b5ac35..a1fedc10112 100644 --- a/src/protocol/mail/src/main/java/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java +++ b/src/protocol/mail/src/main/java/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java @@ -20,6 +20,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Arrays; import java.util.Enumeration; @@ -38,10 +39,12 @@ import javax.mail.internet.AddressException; import javax.mail.internet.ContentType; import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeUtility; import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.CountingOutputStream; import org.apache.commons.io.output.NullOutputStream; +import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.config.ConfigTestElement; import org.apache.jmeter.protocol.smtp.sampler.gui.SecuritySettingsPanel; import org.apache.jmeter.protocol.smtp.sampler.protocol.SendMailCommand; @@ -272,7 +275,7 @@ private SendMailCommand createSendMailCommandFromProperties() throws AddressExce sendMailCmd.setEnableDebug(getPropertyAsBoolean(ENABLE_DEBUG)); if (getPropertyAsString(MAIL_FROM).matches(".*@.*")) { - sendMailCmd.setSender(getPropertyAsString(MAIL_FROM)); + sendMailCmd.setSender(encodeAddress(getPropertyAsString(MAIL_FROM))); } // Process address lists @@ -368,7 +371,7 @@ private List getPropAsAddresses(String propKey) throws AddressE if (!propValue.isEmpty()) { // we have at least one potential address List addresses = new ArrayList<>(); for (String address : propValue.split(";")) { - addresses.add(new InternetAddress(address.trim())); + addresses.add(new InternetAddress(encodeAddress(address))); } return addresses; } else { @@ -376,6 +379,22 @@ private List getPropAsAddresses(String propKey) throws AddressE } } + private String encodeAddress(String address) throws AddressException { + String trimmedAddress = address.trim(); + if (!StringUtils.isAsciiPrintable(trimmedAddress)) { + try { + final int startOfRealAddress = trimmedAddress.indexOf('<'); + if (startOfRealAddress >= 0) { + String personalPart = trimmedAddress.substring(0, startOfRealAddress); + return MimeUtility.encodeWord(personalPart) + trimmedAddress.substring(startOfRealAddress); + } + } catch (UnsupportedEncodingException e) { + log.warn("Can't encode [{}] as quoted printable", trimmedAddress, e); + } + } + return trimmedAddress; + } + /** * @see org.apache.jmeter.samplers.AbstractSampler#applies(org.apache.jmeter.config.ConfigTestElement) */