javaのメールクライアント
メールの閲覧ができるプログラムを作成していますが閲覧ができずエラーが出てしまいます。
正しいソースコードを教えてください。
import java.io.*;
import java.net.*;
public class mail2 {
public static void main(String args[]) throws IOException {
String server,user,pass,str,num;
int port = 110;
StringBuffer sb = new StringBuffer("");
Socket sock;
BufferedReader sockin;
BufferedWriter sockout;
System.out.println("サーバーを入力");
BufferedReader serverin = new BufferedReader(new InputStreamReader(System.in));
server = serverin.readLine();
System.out.println("ユーザーを入力");
BufferedReader userin = new BufferedReader(new InputStreamReader(System.in));
user = userin.readLine();
System.out.println("パスワードを入力");
BufferedReader passin = new BufferedReader(new InputStreamReader(System.in));
pass = passin.readLine();
try {
sock = new Socket(server, port);
sockin = new BufferedReader(new InputStreamReader(sock.getInputStream()));
sockout = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
sockout.write("USER " + user + "\r\n");
sockout.flush();
sockout.write("PASS " + pass + "\r\n");
sockout.flush();
sockout.write("LIST\r\n");
sockout.flush();
sockout.write("QUIT\r\n");
sockout.flush();
while ((str = sockin.readLine()) != null) {
System.out.println(str);
}
sock.close();
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("どのメールを見ますか?");
BufferedReader numin = new BufferedReader(new InputStreamReader(System.in));
num = numin.readLine();
try {
sock = new Socket(server, port);
sockin = new BufferedReader(new InputStreamReader(sock.getInputStream()));
sockout = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
sockout.write("USER " + user + "\r\n");
sockout.flush();
sockout.write("PASS " + pass + "\r\n");
sockout.flush();
sockout.write("LIST\r\n");
sockout.flush();
sockout.write("QUIT\r\n");
sockout.flush();
while ((str = sockin.readLine()) != null) {
System.out.println(str);
}
sock.close();
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("どのメールを見ますか?");
BufferedReader numin = new BufferedReader(new InputStreamReader(System.in));
num = numin.readLine();
try {
sock = new Socket (server, port);
sockin = new BufferedReader(new InputStreamReader(sock.getInputStream()));
sockout = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
sockout.write("USER " + user + "\r\n");
sockout.flush();
sockout.write("PASS " + pass + "\r\n");
sockout.flush();
sockout.write("RETR " +num+"\r\n");
sockout.flush();
sockout.write("QUIT\r\n");
sockout.flush();
while ((str = sockin.readLine()) != null) {
System.out.println(str);
}
sock.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
お礼
分かりやすい解答ありがとうございます!