From 6fb59467b7207a0b1449f35c313ddacf56975b55 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 2 Mar 2010 02:48:52 +0000 Subject: [PATCH] Changed SmtpTransport.writeLine() to only use one OutputStream.write() call. Apparently some servers got the "be liberal in what you accept from others" part of the robustness principle wrong. When we used multiple calls in writeLine() (command + CR + LF) a separate TCP packet was send each time. It appears that those broken servers accepted the DATA command after DATA + CR and interpreted the LF as part of the actual data. This caused our headers to become part of the body because that LF was interpreted as the empty line that separates headers and body. As a side effect of this fix sending mail could be slightly faster now due to less packets being sent. Big thanks to Kevin Newland of Michigan Technological University for organizing a test account. Fixes issue 799 --- .../fsck/k9/mail/transport/SmtpTransport.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/com/fsck/k9/mail/transport/SmtpTransport.java b/src/com/fsck/k9/mail/transport/SmtpTransport.java index bd7008df7..1b152529a 100644 --- a/src/com/fsck/k9/mail/transport/SmtpTransport.java +++ b/src/com/fsck/k9/mail/transport/SmtpTransport.java @@ -414,9 +414,24 @@ public class SmtpTransport extends Transport if (K9.DEBUG) Log.d(K9.LOG_TAG, "SMTP >>> " + s); - mOut.write(s.getBytes()); - mOut.write('\r'); - mOut.write('\n'); + /* + * Note: We can use the string length to compute the buffer size since + * only ASCII characters are allowed in SMTP commands i.e. this string + * will never contain multi-byte characters. + */ + int len = s.length(); + byte[] data = new byte[len + 2]; + s.getBytes(0, len, data, 0); + data[len+0] = '\r'; + data[len+1] = '\n'; + + /* + * Important: Send command + CRLF using just one write() call. Using + * multiple calls will likely result in multiple TCP packets and some + * SMTP servers misbehave if CR and LF arrive in separate pakets. + * See issue 799. + */ + mOut.write(data); mOut.flush(); }