gpt4 book ai didi

JAVA 邮件客户端错误

转载 作者:行者123 更新时间:2023-11-30 10:30:29 28 4
gpt4 key购买 nike

我正在使用 JAVA 套接字连接作为学校项目在端口 25 上为 google 邮件服务器“smtp.gmail.com”构建一个简单的邮件客户端。与服务器的启动和连接都做得很好。但是在发出“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,但据我所知,有些人能够使用普通的套接字并且它对他们有效。我为这个问题搜索了很多但没有任何结果。我不知道发送到服务器的命令中是否缺少某些内容。我真的需要帮助。提前感谢您的宝贵时间。

最佳答案

您是否检查过打开“允许安全性较低的应用程序”并检查此链接中是否缺少任何内容我猜您不能将端口 25 与 SMTP 一起使用

Google mail setup

关于JAVA 邮件客户端错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43671890/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com