SendMail: after '.' that terminates "DATA", send only one CRLF, not two.
[cfb.git] / prod / net / jaekl / qd / util / SendMail.java
index ce21ea3ba81538694d250b3820b8d98463fa5067..68befa72323fd6e8580cf3b77062bf51e99b388d 100644 (file)
@@ -55,7 +55,7 @@ public class SendMail {
        public void send() throws MailException 
        {
                try (
-                               Socket sock = new Socket(m_smtpHost, m_smtpPort);
+                               Socket sock = openSocket(m_smtpHost, m_smtpPort);
                                PrintWriter pw = new PrintWriter(sock.getOutputStream(), true);
                                BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                        ) 
@@ -67,6 +67,10 @@ public class SendMail {
                }
        }
        
+       Socket openSocket(String host, int port) throws UnknownHostException, IOException {
+               return new Socket(host, port);
+       }
+       
        String getHostName() throws UnknownHostException { 
                if (null == m_hostName) {
                        m_hostName = InetAddress.getLocalHost().getHostName();
@@ -84,6 +88,7 @@ public class SendMail {
                        validateResponse("", RESP_220, line);
                        
                        cmd = "HELO " + getHostName();
+                       line = sendLine(pw, br, cmd);
                        validateResponse(cmd, RESP_250, line);
                        
                        cmd = "MAIL FROM: " + m_from;
@@ -124,7 +129,7 @@ public class SendMail {
                
                sendMimeParts(pw);
                
-               String result = sendLine(pw, br, "\r\n.\r\n");
+               String result = sendLine(pw, br, "\r\n.");
                return result;
        }
        
@@ -140,6 +145,7 @@ public class SendMail {
                        sendLine(pw, "Content-Type: " + part.getMimeType());    // TODO:  Add support for encodings
                        sendLine(pw, "");
                        sendLine(pw, part.getContent());
+                       sendLine(pw, "");
                }
                sendLine(pw, "--" + getBoundary() + "--");
        }
@@ -188,7 +194,7 @@ public class SendMail {
                }
        }
        
-       void validateResponse(String cmd, String actual, String expected) throws MailException 
+       void validateResponse(String cmd, String expected, String actual) throws MailException 
        {
                if (! actual.startsWith(expected)) {
                        throw new MailException(cmd, expected, actual);
@@ -280,6 +286,7 @@ public class SendMail {
        void sendLine(PrintWriter pw, String line)
        {
                pw.write(line + "\r\n");
+               pw.flush();
        }
 
        String sendLine(PrintWriter pw, BufferedReader br, String line) throws IOException