Hello,

Here is a patch for org/tools/mail/MailMessage.java that adds the following:

* Support for message encoding (alphabets)
* Fixes to headers for when an optional header hasn't been set (used to send blank headers) (we should check that at least one header from the set: to, cc, bcc, resent-to, resent-cc, resent-bcc exists - but don't at the moment)
* changes to some comments


--
Michael

Index: ant/src/main/org/apache/tools/mail/MailMessage.java
===================================================================
RCS file: /home/cvspublic/ant/src/main/org/apache/tools/mail/MailMessage.java,v
retrieving revision 1.17
diff -u -r1.17 MailMessage.java
--- ant/src/main/org/apache/tools/mail/MailMessage.java 19 Jul 2003 11:20:23 
-0000      1.17
+++ ant/src/main/org/apache/tools/mail/MailMessage.java 2 Aug 2003 17:40:03 
-0000
@@ -66,6 +66,7 @@
 import java.io.PrintStream;
 import java.io.BufferedOutputStream;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 import java.net.Socket;
 import java.net.InetAddress;
 import java.util.Vector;
@@ -131,9 +132,15 @@
  */
 public class MailMessage {
 
+    /** default mailhost */
+    public static final String DEFAULT_HOST = "localhost";
+    
     /** default port for SMTP: 25 */
     public static final int DEFAULT_PORT = 25;
 
+    /** default encoding: iso-8859-1 */
+    public static final String DEFAULT_ENCODING = "iso-8859-1";
+    
     /** host name for the mail server */
     private String host;
 
@@ -161,6 +168,8 @@
 
     private Socket socket;
 
+    private String encoding;
+    
   /**
    * Constructs a new MailMessage to send an email.
    * Use localhost as the mail server with port 25.
@@ -168,7 +177,7 @@
    * @exception IOException if there's any problem contacting the mail server
    */
   public MailMessage() throws IOException {
-    this("localhost", DEFAULT_PORT);
+    this(DEFAULT_HOST, DEFAULT_PORT, DEFAULT_ENCODING);
   }
 
   /**
@@ -179,7 +188,7 @@
    * @exception IOException if there's any problem contacting the mail server
    */
   public MailMessage(String host) throws IOException {
-      this(host, DEFAULT_PORT);
+    this(host, DEFAULT_PORT, DEFAULT_ENCODING);
   }
 
   /**
@@ -191,8 +200,14 @@
    * @exception IOException if there's any problem contacting the mail server
    */
   public MailMessage(String host, int port) throws IOException {
+    this(host, port, DEFAULT_ENCODING);
+  }
+  
+  public MailMessage(String host, int port, String encoding)
+  throws IOException, UnsupportedEncodingException {
     this.port = port;
     this.host = host;
+    this.encoding = encoding;
     replyto = new Vector();
     to = new Vector();
     cc = new Vector();
@@ -299,19 +314,30 @@
     return out;
   }
 
+  
+  // RFC 822 s4.1: "From:" header must be sent
+  // We rely on error checking by the MTA
   void setFromHeader() {
     setHeader("From", from);
   }
 
+  // RFC 822 s4.1: "Reply-To:" header is optional
   void setReplyToHeader() {
+    if ( ! replyto.isEmpty() ) {
       setHeader("Reply-To", vectorToList(replyto));
+    }
   }
+  
   void setToHeader() {
-    setHeader("To", vectorToList(to));
+    if ( ! to.isEmpty() ) {
+      setHeader("To", vectorToList(to));
+    }
   }
 
   void setCcHeader() {
-    setHeader("Cc", vectorToList(cc));
+    if ( ! cc.isEmpty() ) {
+      setHeader("Cc", vectorToList(cc));
+    }
   }
 
   String vectorToList(Vector v) {
@@ -327,7 +353,10 @@
   }
 
   void flushHeaders() throws IOException {
-    // XXX Should I care about order here?
+    // RFC 822 s4.1:
+    //   "Header fields are NOT required to occur in any particular order,
+    //    except that the message body MUST occur AFTER the headers"
+    // (the same section specifies a reccommended order, which we ignore)
     Enumeration e = headers.keys();
     while (e.hasMoreElements()) {
       String name = (String) e.nextElement();
@@ -389,11 +418,12 @@
 
   // * * * * * Raw protocol methods below here * * * * *
 
-  void connect() throws IOException {
+  void connect() throws IOException, UnsupportedEncodingException {
     socket = new Socket(host, port);
     out = new MailPrintStream(
           new BufferedOutputStream(
-          socket.getOutputStream()));
+          socket.getOutputStream()),
+          encoding);
     in = new SmtpResponseReader(socket.getInputStream());
     getReady();
   }
@@ -493,6 +523,12 @@
     super(out, true);  // deprecated, but email is byte-oriented
   }
 
+  public MailPrintStream(OutputStream out, String encoding)
+  throws UnsupportedEncodingException
+  {
+    super(out, true, encoding);  // deprecated, but email is byte-oriented
+  }
+  
   // Mac does \n\r, but that's tough to distinguish from Windows \r\n\r\n.
   // Don't tackle that problem right now.
   public void write(int b) {

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to