Hi Juergen!

A few days ago you answered my question concerning javaīs
mailto-protocol and the exception produced by the appended source:

java.net.UnknownHostException: mailhost

   Juergen> Sounds like a setup problem. Can you ping mailhost?

No. Another nice GUY suggested to mention the localhost as
mailhost in /etc/hosts. Appending the line "127.0.0.1 mailhost"
I was able to ping the mailhost. But that seems just to shift
the problem. Executing the program now leads to a
ConnectionRefused-Exception ...

You said the appended source works fine with your machine
and I agree that it most probably will be a setup-problem with
the mail-deamon. Could you just mail me the corresponding
line of your /etc/hosts? As far as I read the hosts-manpage
this line will customize the behavior of the mailhost.
This method might keep me out of irrelevant details ;-)

Christian

*********************** SendMail.java ****************************

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

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) { 
      System.err.println(e);
      System.err.println("Usage: java SendMail [<mailhost>]");
    }
  }
}


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to