尝试在没有javamail的情况下打开gmail文件夹
所以我创建了一个程序,没有使用javamail连接gmail IMAP。我已成功登录,但我正在努力找到打开文件夹的方法。我已经看过了所有的答案都是javamail,我不想使用它,因为我自己也在学习它,但是我似乎在JavaAPI中找不到任何允许我在不使用javamail的情况下打开此文件的东西 这是我的密码:尝试在没有javamail的情况下打开gmail文件夹,java,imap,directory,Java,Imap,Directory,所以我创建了一个程序,没有使用javamail连接gmail IMAP。我已成功登录,但我正在努力找到打开文件夹的方法。我已经看过了所有的答案都是javamail,我不想使用它,因为我自己也在学习它,但是我似乎在JavaAPI中找不到任何允许我在不使用javamail的情况下打开此文件的东西 这是我的密码: public class CRole { private BufferedReader socketSIn = null; private PrintWriter sock
public class CRole {
private BufferedReader socketSIn = null;
private PrintWriter socketSOut = null;
private Socket client;
public CRole(){
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
try {
client = (SSLSocket) sslsocketfactory.createSocket("imap.gmail.com", 993);
System.out.println("Connect to Host");
}
catch(IOException e) {
System.out.println("Unable to listen on ports");
System.exit(+1);
}
try {
socketSIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
socketSOut = new PrintWriter(client.getOutputStream(), true);
}
catch(IOException e) {
System.out.println("Read failed");
System.exit(+1);
}
}
public void send_connectStringToS(String payload) {
this.socketSOut.println(payload);
}
public String receive_acceptedStringFromS() {
String line = "";
try {
line = this.socketSIn.readLine();
}
catch(IOException e) {
System.out.println("Input/Outpur error.");
System.exit(+1);
}
return line;
}
有更多的代码,但我不认为这个问题真的需要它。基本上,我必须使用javamail还是有其他选择
还有一种更快的缩进方式,因为当我使用空格键时,它总是避开
public class Imaps {
private static BufferedReader in = null;
private static PrintWriter out = null;
private static Socket client;
public static void main (String []arg){
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
try {
client = (SSLSocket) sslsocketfactory.createSocket("imap.gmail.com", 993);
System.out.println("Connect to Host");
}
catch(IOException e) {
System.out.println("Unable to listen on ports");
System.exit(+1);
}
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
out.println("abcd CAPABILITY");
String response = (String) in.readLine();
System.out.println("Server: " + response);
out.println("efgh STARTTLS"); //tls part does not work but will not cause any problems being there.
response = (String) in.readLine();
System.out.println("Server: " + response);
String payload3 = "a001 login username @gmail.com password";
out.println(payload3);
response = (String) in.readLine();
System.out.println("Server: " + response);
response = (String) in.readLine();
System.out.println("Server: " + response);
response = (String) in.readLine();
System.out.println("Server: " + response);
//should say success if you enter correct password.
String payload5 = "a002 select inbox";
//String payload5 = "a002 select inbox";
out.println(payload5);
response = (String) in.readLine();
System.out.println("Server: " + response);
response = (String) in.readLine();
System.out.println("Server: " + response);
response = (String) in.readLine();
System.out.println("Server: " + response);
response = (String) in.readLine();
System.out.println("Server: " + response);
}
catch(IOException e) {
System.out.println("Read failed");
System.exit(+1);
}
答案是,发送a002 select inbox时,不需要将文件夹存储在打开的任何位置。在此之后,您可以使用a004 fetch 1 body[文本]请求任何您想要的消息。请注意,第一名将获取您帐户中的第一封电子邮件,而不是最近的一封 如果您正在寻找一个API,您已经猜到了,Javamail是一个不错的选择。否则,您可以尝试创建自己的IMAP实现,祝您好运。如果您真的想从头开始,实际的协议记录在RFC3501中。是的,我已经阅读了一段时间,不知道该做什么。然而,这比我想象的要容易得多。