Merge pull request #5146 from k9mail/fix_smtp_message_reject

SMTP: Read all responses before issuing DATA command
This commit is contained in:
cketti 2021-02-16 17:11:00 +01:00 committed by GitHub
commit ad83acb05a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 37 deletions

View file

@ -413,7 +413,6 @@ public class SmtpTransport extends Transport {
pipelinedCommands.add(String.format("RCPT TO:<%s>", address));
}
pipelinedCommands.add("DATA");
executePipelinedCommands(pipelinedCommands);
readPipelinedResponse(pipelinedCommands);
} else {
@ -422,10 +421,10 @@ public class SmtpTransport extends Transport {
for (String address : addresses) {
executeCommand("RCPT TO:<%s>", address);
}
executeCommand("DATA");
}
executeCommand("DATA");
EOLConvertingOutputStream msgOut = new EOLConvertingOutputStream(
new LineWrapOutputStream(new SmtpDataStuffing(outputStream), 1000));
@ -618,35 +617,23 @@ public class SmtpTransport extends Transport {
private void readPipelinedResponse(Queue<String> pipelinedCommands) throws IOException, MessagingException {
String responseLine;
List<String> results = new ArrayList<>();
NegativeSmtpReplyException firstNegativeResponse = null;
boolean dataCommandOk = true;
for (String command : pipelinedCommands) {
MessagingException firstException = null;
for (int i = 0, size = pipelinedCommands.size(); i < size; i++) {
results.clear();
responseLine = readCommandResponseLine(results);
try {
responseLineToCommandResponse(responseLine, results);
} catch (MessagingException exception) {
if (command.equals("DATA")) {
dataCommandOk = false;
}
if (firstNegativeResponse == null) {
firstNegativeResponse = (NegativeSmtpReplyException) exception;
if (firstException == null) {
firstException = exception;
}
}
}
if (firstNegativeResponse != null) {
try {
if (dataCommandOk) {
executeCommand(".");
}
throw firstNegativeResponse;
} catch (NegativeSmtpReplyException e) {
throw firstNegativeResponse;
}
if (firstException != null) {
throw firstException;
}
}
private CommandResponse responseLineToCommandResponse(String line, List<String> results) throws MessagingException {

View file

@ -692,9 +692,9 @@ public class SmtpTransportTest {
MockSmtpServer server = createServerAndSetupForPlainAuthentication("PIPELINING");
server.expect("MAIL FROM:<user@localhost>");
server.expect("RCPT TO:<user2@localhost>");
server.output("250 OK");
server.output("250 OK");
server.expect("DATA");
server.output("250 OK");
server.output("250 OK");
server.output("354 End data with <CR><LF>.<CR><LF>");
server.expect("[message data]");
server.expect(".");
@ -740,12 +740,8 @@ public class SmtpTransportTest {
MockSmtpServer server = createServerAndSetupForPlainAuthentication("PIPELINING");
server.expect("MAIL FROM:<user@localhost>");
server.expect("RCPT TO:<user2@localhost>");
server.expect("DATA");
server.output("250 OK");
server.output("550 remote mail to <user2@localhost> not allowed");
server.output("354 End data with <CR><LF>.<CR><LF>");
server.expect(".");
server.output("554 no valid recipients");
server.expect("QUIT");
server.output("221 BYE");
server.closeConnection();
@ -770,10 +766,8 @@ public class SmtpTransportTest {
MockSmtpServer server = createServerAndSetupForPlainAuthentication("PIPELINING");
server.expect("MAIL FROM:<user@localhost>");
server.expect("RCPT TO:<user2@localhost>");
server.expect("DATA");
server.output("250 OK");
server.output("550 remote mail to <user2@localhost> not allowed");
server.output("554 no valid recipients given");
server.expect("QUIT");
server.output("221 BYE");
server.closeConnection();
@ -798,13 +792,9 @@ public class SmtpTransportTest {
server.expect("MAIL FROM:<user@localhost>");
server.expect("RCPT TO:<user2@localhost>");
server.expect("RCPT TO:<user3@localhost>");
server.expect("DATA");
server.output("250 OK");
server.output("550 remote mail to <user2@localhost> not allowed");
server.output("550 remote mail to <user3@localhost> not allowed");
server.output("354 End data with <CR><LF>.<CR><LF>");
server.expect(".");
server.output("554 no valid recipients given");
server.expect("QUIT");
server.output("221 BYE");
server.closeConnection();
@ -829,13 +819,9 @@ public class SmtpTransportTest {
server.expect("MAIL FROM:<user@localhost>");
server.expect("RCPT TO:<user2@localhost>");
server.expect("RCPT TO:<user3@localhost>");
server.expect("DATA");
server.output("250 OK");
server.output("250 OK");
server.output("550 remote mail to <user3@localhost> not allowed");
server.output("354 End data with <CR><LF>.<CR><LF>");
server.expect(".");
server.output("250 OK");
server.expect("QUIT");
server.output("221 BYE");
server.closeConnection();