0

This textbook example is giving me an UnknownHostException. I'm putting my email address as the console arguments like this : java MailClient ivan.ivanhoe@yahoo.com Could someone give me a brief explanation why it's not possible, or advise me how to run/rewrite the application so it can send mail. Thanks xx

//MailClient.java
//Tries to send an email to my address : UnknownHostException. Trying to sort it now....

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

public class MailClient  {

    public static void main (String[] args)  {

        if (args.length == 0)  {
            System.err.println("Usage : java MailClient email@host.com");
            return;
        }

        try  {
            URL u = new URL ("mailto:" + args[0]);
            URLConnection uc = u.openConnection();
            uc.setDoOutput(true);
            uc.connect();
            OutputStream out = uc.getOutputStream();
            StreamCopier.copy(System.in, out);
            out.close();
        }  catch (IOException e)  {
            System.err.print(e);
        }

    } //end main
}



import java.io.*;

public class StreamCopier  {

    public static void main (String[] args)  {

        try  {
            copy (null, null);
        } catch (IOException e) {
            System.err.println(e);
        }
    }

    public static void copy (InputStream in, OutputStream out) throws IOException  {

        //do not allow other threads to read from the  input or
        //write to the output while copying is taking place.
        synchronized (in)  {
            synchronized (out)  {
                byte [] buffer = new byte [256];
                while (true)   {
                    int bytesRead = in.read(buffer);
                    if (bytesRead == -1) 
                        break;
                    out.write(buffer, 0, bytesRead);
                }
            }
        }
    }  //end copy()

} 
Luffy
  • 1,317
  • 1
  • 19
  • 41
user3814983
  • 39
  • 2
  • 8
  • where does `StreamCopier`come from? – Seismoid Aug 30 '14 at 00:06
  • StreamCopier is in the same directory as MailClient. I'll post its contents up now. – user3814983 Aug 30 '14 at 00:20
  • the only thing i can tell you is that it will not work like this, opening a connection and writing to it. i can see no host server (like smtp.gmail.com), no port, no authentification (username+password); you should better download an api like JavaMail and use that (tutoril at http://www.tutorialspoint.com/javamail_api/javamail_api_sending_simple_email.htm) – Seismoid Aug 30 '14 at 00:52
  • This looks like Java to me, but you've tagged it .NET! Please make up your mind. – RenniePet Aug 30 '14 at 03:05
  • I meant the java.net classes. – user3814983 Aug 30 '14 at 03:39
  • `URL u = new URL ("mailto:" + args[0]);` doesn't make any sens, `mailto` is used to actually open a mail client. You can't open this type of URL, you won't connect to any server for communication. – Alexandre Lavoie Aug 30 '14 at 04:03

0 Answers0