我正在嘗試在java中使用帶有IMAP的SSL。我不想使用IMAP類。 出於某種原因,當我發送第n條消息時,我收到消息n-2的答案,而不是消息n-1。這意味着在發送第二條消息之前,我沒有收到任何第一條消息的答覆。任何人都可以發現以下最小代碼中的錯誤嗎? (除了println之外,它確實是最小的,我想幫助調試)Java,SSLSocket,未接收到任何答案
import java.io.*;
import javax.net.ssl.*;
public class Mail{
static String server = "imap.gmail.com";
static String user = "[email protected]";
static String pass = "azerty75";
public static void print (PrintWriter to, String text){
System.out.println("sent : "+text);
to.println(text+ "\r");
to.flush();
}
public static void read (BufferedReader from) throws InterruptedException, IOException {
do {
String line = from.readLine();
System.out.println("received: "+line);
} while (from.ready());
}
public static void main(String[] args){
try{
SSLSocket sslsocket = (SSLSocket) SSLSocketFactory.getDefault().createSocket(server, 993);
System.out.println("Start connexion");
BufferedReader from = new BufferedReader(new InputStreamReader(sslsocket.getInputStream()));
// read(from);
PrintWriter to = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sslsocket.getOutputStream())), true);
print(to,"a1 login "+user+" "+pass);
read(from);/*exepcted:
OK gimap ready
a1 OK [email protected] authenticated (Success)*/
sslsocket.close();
System.out.println("End connexion");
}
catch (Exception e){
e.printStackTrace();
}
}
}
你爲什麼不使用JavaMail呢? – EJP
您確定要發送CRLF對嗎?而不只是換行? – Max
@EJP我的目標之一就是不需要套接字,特別是安全套接字 –