这个教科书示例给了我一个UnknownHostException
。我将我的电子邮件地址作为控制台参数,如下所示:java MailClient ivan.ivanhoe@yahoo.com
有人可以给我一个简短的解释,为什么它不可能,或建议我如何运行/重写应用程序,以便它可以发送邮件。谢谢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()
}