Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4191994

URLConnection using "mailto:" fails with ESMTP on Sun

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.1.6
    • core-libs
    • merlin
    • x86
    • windows_nt



      Name: jn10789 Date: 11/20/98


      This is a standard example in the Java Examples in a Nutshell,
      page 188.

      When you do a c.Connect(),
      > > > The example invisibly sends
      > > > helo machinename
      > > > mail from: username@machinename
      > > > data
      > > >
      > > > which fails for many SMTP servers which don't allow data until an
      > > > RCPT from: has been given too.
      > > >
      > > > I got to the bottom of this using a heavily edited SendMail example,
      > > > the GenericClient example and by modifying the HttpMirror example to
      > > > mimic an SMTP server, and by playing with a telnet session to a real
      > > > ESMTP server (SendMail 8.8.8/8.8.8) running on a Sun.


      // This example is from _Java Examples in a Nutshell_. (http://www.oreilly.com)
      // Copyright (c) 1997 by David Flanagan
      // This example is provided WITHOUT ANY WARRANTY either expressed or implied.
      // You may study, use, modify, and distribute it for non-commercial purposes.
      // For any commercial use, see http://www.davidflanagan.com/javaexamples

      import java.io.*;
      import java.net.*;

      /**
       * This program sends e-mail using a mailto: URL
       **/
      public class SendMail {
        public static void main(String[] args) {
          try {
            // If the user specified a mailhost, tell the system about it.
            if (args.length >= 1) System.getProperties().put("mail.host", args[0]);

            // A Reader stream to read from the console
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

            // Ask the user for the from, to, and subject lines
            System.out.print("From: ");
            String from = in.readLine();
            System.out.print("To: ");
            String to = in.readLine();
            System.out.print("Subject: ");
            String subject = in.readLine();

            // Establish a network connection for sending mail
            URL u = new URL("mailto:" + to); // Create a mailto: URL
            URLConnection c = u.openConnection(); // Create a URLConnection for it
            c.setDoInput(false); // Specify no input from this URL
            c.setDoOutput(true); // Specify we'll do output
            System.out.println("Connecting..."); // Tell the user what's happening
            System.out.flush(); // Tell them right now
            c.connect(); // Connect to mail host
            PrintWriter out = // Get output stream to mail host
              new PrintWriter(new OutputStreamWriter(c.getOutputStream()));

            // Write out mail headers. Don't let users fake the From address
            out.println("From: \"" + from + "\" <" +
                        System.getProperty("user.name") + "@" +
                        InetAddress.getLocalHost().getHostName() + ">");
            out.println("To: " + to);
            out.println("Subject: " + subject);
            out.println(); // blank line to end the list of headers

            // Now ask the user to enter the body of the message
            System.out.println("Enter the message. " +
                               "End with a '.' on a line by itself.");
            // Read message line by line and send it out.
            String line;
            for(;;) {
              line = in.readLine();
              if ((line == null) || line.equals(".")) break;
              out.println(line);
            }

            // Close the stream to terminate the message
            out.close();
            // Tell the user it was successfully sent.
            System.out.println("Message sent.");
            System.out.flush();
          }
          catch (Exception e) { // Handle any exceptions, print error message.
            System.err.println(e);
            System.err.println("Usage: java SendMail [<mailhost>]");
          }
        }
      }
      (Review ID: 36029)
      ======================================================================

            jccollet Jean-Christophe Collet (Inactive)
            jdn Jeffrey Nisewanger (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: