]> jaekl.net Git - cfb.git/commitdiff
SendMail: make charset expected on socket reads explicit.
authorChris Jaekl <cejaekl@yahoo.com>
Sun, 1 Nov 2015 02:23:46 +0000 (11:23 +0900)
committerChris Jaekl <cejaekl@yahoo.com>
Sun, 1 Nov 2015 02:23:46 +0000 (11:23 +0900)
prod/net/jaekl/qd/util/SendMail.java

index 68befa72323fd6e8580cf3b77062bf51e99b388d..dc65e4fdca9ce84f30e7aae751805795b4bbaeb5 100644 (file)
@@ -7,6 +7,7 @@ import java.io.PrintWriter;
 import java.net.InetAddress;
 import java.net.Socket;
 import java.net.UnknownHostException;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -17,8 +18,17 @@ public class SendMail {
        private static final String RESP_251 = "251";   // mail address change (we don't support this)
        private static final String RESP_354 = "354";   // OK, proceed with DATA transmission
        
+       // Actually, RFC821 (with which all SMTP should be backwards-compatible)
+       // requires all control transmissions to be done in US-ASCII.
+       // However, there's no point in throwing an exception if the server with which 
+       // we're communicating sends bytes with the high bit set.  So, let's read from 
+       // the socket with the only Charset that Java guarantees will be available that 
+       // can also read arbitrary sequences of 8-bit bytes.
+       private static final String SMTP_CHARSET = "ISO-8859-1";
+       
        String m_hostName;
        String m_boundary;
+       Charset m_charset;
        
        String m_smtpHost;
        int m_smtpPort;
@@ -57,7 +67,7 @@ public class SendMail {
                try (
                                Socket sock = openSocket(m_smtpHost, m_smtpPort);
                                PrintWriter pw = new PrintWriter(sock.getOutputStream(), true);
-                               BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
+                               BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream(), getCharset()));
                        ) 
                {
                        send(pw, br);
@@ -159,6 +169,13 @@ public class SendMail {
                return true;
        }
        
+       Charset getCharset() {
+               if (null == m_charset) {
+                       m_charset = Charset.forName(SMTP_CHARSET);
+               }
+               return m_charset;
+       }
+       
        String getBoundary()
        {
                if (null != m_boundary) {