JAVA 邮件客户端错误
JAVA mail client errors
我正在为 google 邮件服务器“smtp.gmail.com”在端口 25 上使用 JAVA 套接字连接构建一个简单的邮件客户端作为学校项目。与服务器的启动和连接都做得很好。但是在发出“STARTTLS”命令后进行身份验证时,我收到了一个奇怪的回复,有时服务器没有回复。
以下是导致此错误的代码部分:
public class Client {
public static void main(String[] args) throws IOException {
System.out.println("Connecting to server...");
Socket cs = new Socket("smtp.gmail.com", 25);
System.out.println("Connected to server! via port: " + cs.getLocalPort());
OutputStreamWriter osw = new OutputStreamWriter(cs.getOutputStream());
InputStreamReader isr = new InputStreamReader(cs.getInputStream());
BufferedWriter bw = new BufferedWriter(osw);
char[] inputBuffer = new char[1024];
int len = isr.read(inputBuffer);
String s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + s);
System.out.println("C: HELO localhost");
bw.write("HELO localhost");
bw.newLine();
bw.flush();
len = isr.read(inputBuffer);
s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + s);
System.out.println("C: STARTTLS");
bw.write("STARTTLS");
bw.newLine();
bw.flush();
len = isr.read(inputBuffer);
s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + s);
System.out.println("C: EHLO localhost");
bw.write("EHLO localhost");
bw.newLine();
bw.flush();
len = isr.read(inputBuffer);
s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + s);
System.out.println("C: AUTH LOGIN");
bw.write("AUTH LOGIN");
bw.newLine();
bw.flush();
len = isr.read(inputBuffer);
s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + s);
// extra code goes here, no need to show
}
}
输出如下:
Connecting to server...
Connected to server! via port: 39167
S: 220 smtp.gmail.com ESMTP v52sm3768873wrc.53 - gsmtp
C: HELO localhost
S: 250 smtp.gmail.com at your service
C: STARTTLS
S: 220 2.0.0 Ready to start TLS
C: EHLO localhost
S: F
C: AUTH LOGIN
S:
如您所见,“STARTTLS”后的“EHLO localhost”回复不明确,之后“AUTH LOGIN”没有回复。我一开始以为应该是服务端发送的是Base64文本,所以我试着解码响应如下:
System.out.println("C: EHLO localhost");
bw.write("EHLO localhost");
bw.newLine();
bw.flush();
len = isr.read(inputBuffer);
s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + new String(Base64.getDecoder().decode(s)));
System.out.println("C: AUTH LOGIN");
bw.write("AUTH LOGIN");
bw.newLine();
bw.flush();
len = isr.read(inputBuffer);
s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + new String(Base64.getDecoder().decode(s)));
但是得到如下错误:
Connecting to server...
Connected to server! via port: 39199
S: 220 smtp.gmail.com ESMTP l27sm4475707wrb.65 - gsmtp
C: HELO localhost
S: 250 smtp.gmail.com at your service
C: STARTTLS
S: 220 2.0.0 Ready to start TLS
C: EHLO localhost
Exception in thread "main" java.lang.IllegalArgumentException: Illegal base64 character 15
at java.util.Base64$Decoder.decode0(Unknown Source)
at java.util.Base64$Decoder.decode(Unknown Source)
at java.util.Base64$Decoder.decode(Unknown Source)
at khaldoun.kassam.servertest.Client.main(Client.java:64)
我认为也许我应该使用 SSLSockets,但据我所知,有些人能够使用普通的套接字并且它对他们有效。我为这个问题搜索了很多但没有任何结果。我不知道发送到服务器的命令中是否缺少某些内容。我真的需要帮助。
预先感谢您的宝贵时间。
你检查过打开 "Allow less secure Apps" 检查这里有什么遗漏吗 link 另外我猜你不能将端口 25 用于 SMTP
我正在为 google 邮件服务器“smtp.gmail.com”在端口 25 上使用 JAVA 套接字连接构建一个简单的邮件客户端作为学校项目。与服务器的启动和连接都做得很好。但是在发出“STARTTLS”命令后进行身份验证时,我收到了一个奇怪的回复,有时服务器没有回复。 以下是导致此错误的代码部分:
public class Client {
public static void main(String[] args) throws IOException {
System.out.println("Connecting to server...");
Socket cs = new Socket("smtp.gmail.com", 25);
System.out.println("Connected to server! via port: " + cs.getLocalPort());
OutputStreamWriter osw = new OutputStreamWriter(cs.getOutputStream());
InputStreamReader isr = new InputStreamReader(cs.getInputStream());
BufferedWriter bw = new BufferedWriter(osw);
char[] inputBuffer = new char[1024];
int len = isr.read(inputBuffer);
String s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + s);
System.out.println("C: HELO localhost");
bw.write("HELO localhost");
bw.newLine();
bw.flush();
len = isr.read(inputBuffer);
s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + s);
System.out.println("C: STARTTLS");
bw.write("STARTTLS");
bw.newLine();
bw.flush();
len = isr.read(inputBuffer);
s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + s);
System.out.println("C: EHLO localhost");
bw.write("EHLO localhost");
bw.newLine();
bw.flush();
len = isr.read(inputBuffer);
s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + s);
System.out.println("C: AUTH LOGIN");
bw.write("AUTH LOGIN");
bw.newLine();
bw.flush();
len = isr.read(inputBuffer);
s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + s);
// extra code goes here, no need to show
}
}
输出如下:
Connecting to server...
Connected to server! via port: 39167
S: 220 smtp.gmail.com ESMTP v52sm3768873wrc.53 - gsmtp
C: HELO localhost
S: 250 smtp.gmail.com at your service
C: STARTTLS
S: 220 2.0.0 Ready to start TLS
C: EHLO localhost
S: F
C: AUTH LOGIN
S:
如您所见,“STARTTLS”后的“EHLO localhost”回复不明确,之后“AUTH LOGIN”没有回复。我一开始以为应该是服务端发送的是Base64文本,所以我试着解码响应如下:
System.out.println("C: EHLO localhost");
bw.write("EHLO localhost");
bw.newLine();
bw.flush();
len = isr.read(inputBuffer);
s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + new String(Base64.getDecoder().decode(s)));
System.out.println("C: AUTH LOGIN");
bw.write("AUTH LOGIN");
bw.newLine();
bw.flush();
len = isr.read(inputBuffer);
s = "";
for(int i = 0; i < len; ++i){
s += inputBuffer[i];
}
System.out.println("S: " + new String(Base64.getDecoder().decode(s)));
但是得到如下错误:
Connecting to server...
Connected to server! via port: 39199
S: 220 smtp.gmail.com ESMTP l27sm4475707wrb.65 - gsmtp
C: HELO localhost
S: 250 smtp.gmail.com at your service
C: STARTTLS
S: 220 2.0.0 Ready to start TLS
C: EHLO localhost
Exception in thread "main" java.lang.IllegalArgumentException: Illegal base64 character 15
at java.util.Base64$Decoder.decode0(Unknown Source) at java.util.Base64$Decoder.decode(Unknown Source) at java.util.Base64$Decoder.decode(Unknown Source) at khaldoun.kassam.servertest.Client.main(Client.java:64)
我认为也许我应该使用 SSLSockets,但据我所知,有些人能够使用普通的套接字并且它对他们有效。我为这个问题搜索了很多但没有任何结果。我不知道发送到服务器的命令中是否缺少某些内容。我真的需要帮助。 预先感谢您的宝贵时间。
你检查过打开 "Allow less secure Apps" 检查这里有什么遗漏吗 link 另外我猜你不能将端口 25 用于 SMTP