Ошибка Connection reset протокол SMTP
Есть простой SMTP клиент, который лишь должен отправлять команды на сервер SMTP и получать код ответа и сообщение. Все бы ничего, но по какой-то причине дальше команды STARTTLS клиент отказывается работать с ошибкой Connection reset. В чем может быть ошибка? Привожу ниже код:
import java.net.*;
import java.io.*;
import java.util.Objects;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class EmailClient {
public static void main(String[] args) {
String host = "smtp.gmail.com";
int port = 587;
String from = "[email protected]";
String to = "[email protected]";
String subject = "Test email";
String body = "This is a test email.";
try {
// Create a socket connection to the mail server
Socket socket = new Socket(host, port);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
// Read the server's greeting
String response = in.readLine();
System.out.println(response);
// Send the EHLO command to initiate the SMTP conversation
out.println("EHLO " + host);
out.flush();
do {
response = in.readLine();
System.out.println(response);
}while (!Objects.equals(response, "250 SMTPUTF8"));
// Start a secure TLS connection
out.println("STARTTLS");
out.flush();
response = in.readLine();
System.out.println(response);
SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket, host, port, true);
in = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(sslSocket.getOutputStream()));
// Set the sender and recipient of the email
out.println("MAIL FROM:<" + from + ">");
out.flush();
response = in.readLine();
System.out.println(response);
out.println("RCPT TO:<" + to + ">");
out.flush();
response = in.readLine();
System.out.println(response);
// Send the email content
out.println("DATA");
out.flush();
response = in.readLine();
System.out.println(response);
out.println("Subject:" + subject);
out.println("");
out.println(body);
out.println(".");
out.flush();
response = in.readLine();
System.out.println(response);
// Close the connection
out.println("QUIT");
out.flush();
response = in.readLine();
System.out.println(response);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Ответы (1 шт):
Автор решения: void
→ Ссылка
У вас порт указан 25. Смените порт с 25 на 587.
int port = 587;
Исходящий SMTP-сервер smtp.gmail.com поддерживает TLS. Если ваш клиент начинается с простого текста, перед выполнением команды STARTTLS используйте порт 465 (для SSL) или порт 587 (для TLS).
(c) Microsoft Corporation. All rights reserved.
C:\Users\xxx>telnet smtp.gmail.com 25
Connecting To smtp.gmail.com...Could not open connection to the host, on port 25: Connect failed
C:\Users\xxx>telnet smtp.gmail.com 587
$ openssl s_client -starttls smtp -connect smtp.gmail.com:587 -crlf
CONNECTED(000001A0)
---
Certificate chain
0 s:CN = smtp.gmail.com
i:C = US, O = Google Trust Services LLC, CN = GTS CA 1C3
1 s:C = US, O = Google Trust Services LLC, CN = GTS CA 1C3
i:C = US, O = Google Trust Services LLC, CN = GTS Root R1
2 s:C = US, O = Google Trust Services LLC, CN = GTS Root R1
i:C = BE, O = GlobalSign nv-sa, OU = Root CA, CN = GlobalSign Root CA
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIEiTCCA3GgAwIBAgIRALtoYgdHbzujCqo/4U31j6wwDQYJ
OFZEIuFvOgPE1R6GtZSjDT5GgTHP+1ffkdBBlQJYd/jICN8IajGUykfhlnkthxRo
Qjf7D7BM3XWKSsi2u3Z4+NIxxg8CX9R3k0GbxNZxQqjIvypsWSW+X1/9O0XAS67r
RyLOz/nExZIzNpgDBaqK6UC1oY4iOQz6V57R315RNUSGvtiNGMq5QvJyl1l9IyLP
QxynB1E/NpyNhZ4sWA==
-----END CERTIFICATE-----
subject=CN = smtp.gmail.com
issuer=C = US, O = Google Trust Services LLC, CN = GTS CA 1C3
---
No client certificate CA names sent
Peer signing digest: SHA256
Peer signature type: ECDSA
Server Temp Key: X25519, 253 bits
---
SSL handshake has read 4583 bytes and written 429 bytes
Verification: OK
---
New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
Server public key is 256 bit
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 0 (ok)
---
