Java 如何打印所有收件箱电子邮件
我对Java非常陌生,对端口、服务器或套接字一无所知,所以我希望有人能帮助我。我想做一个小程序,将打印所有的邮件在您的收件箱(读和未读) 我在网上找到了这段代码,但它只给我发送文件夹中最早的邮件Java 如何打印所有收件箱电子邮件,java,sockets,ssl,imap,ports,Java,Sockets,Ssl,Imap,Ports,我对Java非常陌生,对端口、服务器或套接字一无所知,所以我希望有人能帮助我。我想做一个小程序,将打印所有的邮件在您的收件箱(读和未读) 我在网上找到了这段代码,但它只给我发送文件夹中最早的邮件 public class EmailService { String server = "pop.gmail.com"; int port = 995; static String username; static String password; SSLSocket socket
public class EmailService {
String server = "pop.gmail.com";
int port = 995;
static String username;
static String password;
SSLSocket socket;
BufferedReader input;
PrintWriter output;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
username = s.next() + "@gmail.com";
password = s.next();
new EmailService();
}
public EmailService() {
try {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
socket = (SSLSocket)sslsocketfactory.createSocket(server, port);
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// After each println you MUST flush the buffer, or it won't work properly.
// The true argument makes an automatic flush after each println.
output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
connect();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void connect() throws IOException {
System.out.print("Greeting message: ");
String response = readOneLine();
System.out.println(response);
// Username
output.println("USER " + username);
response = readOneLine();
System.out.println(response);
// Password
output.println("PASS " + password);
response = readOneLine();
System.out.println(response);
output.println("RETR 1");
while (!response.equals(".")) {
response = readOneLine();
System.out.println(response);
}
}
public String readOneLine() throws IOException {
return input.readLine();
}
}
如何更改此代码以获取收件箱中的所有邮件?无需编写此类低级代码。改用JavaMail库。Dnault有个好主意。它的效率相当高。网络解决方案多年来一直在使用它向我和数百万其他人发送垃圾邮件。他们不是最聪明的一群(即使在我起诉他们之后,他们也无法停止),所以它也必须易于使用。