我正在尝试使用 javax.mail 来制作发送邮件的应用程序。
我一直试图弄清楚如何解决这个错误No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
我将在我不控制操作系统的系统上使用它,因此我需要遵守默认的 rhel8 策略。
- 这将在 Windows 10 机器上运行。
- 仅当我使用以下命令时,它才能在我的 rhel8 测试系统中工作:
update-crypto-policies --set LEGACY
我需要在代码中解决这个问题,而不是操作系统。
我在 rhel8 上遇到的错误:
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 SMTP server ready
EHLO localhost.localdomain
javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1420)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1408)
at com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:847)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:384)
at javax.mail.Service.connect(Service.java:297)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at MailTest.sendMail(MailTest.java:59)
at MailTest.main(MailTest.java:103)
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:171)
at sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:98)
at sun.security.ssl.TransportContext.kickstart(TransportContext.java:220)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:428)
at sun.security.ssl.SSLSocketImpl.ensureNegotiated(SSLSocketImpl.java:804)
at sun.security.ssl.SSLSocketImpl.access$200(SSLSocketImpl.java:73)
at sun.security.ssl.SSLSocketImpl$AppOutputStream.write(SSLSocketImpl.java:1166)
at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:101)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1418)
... 10 more
我试过:
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
props.put("mail.smtp.ssl.protocols", "TLSv1.3");
- 没有
mail.smtp.ssl.protocols
条目
我的代码:
import com.sun.mail.smtp.SMTPSendFailedException;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.AuthenticationFailedException;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* Standalone mail client.
*/
public class MailTest {
private final Properties props;
public MailTest(Properties props) {
this.props = props;
}
public void sendMail() {
try {
Authenticator auth = null;
final String username = props.getProperty("mail.username");
final String password = props.getProperty("mail.password");
if (username != null && username.length() > 0) {
auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("Providing authentication for: " + username);
return new PasswordAuthentication(username, password);
}
};
}
Session session = Session.getInstance(props, auth);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress(props.getProperty("test.from"), "test"));
msg.setReplyTo(InternetAddress.parse(props.getProperty("test.from"), false));
msg.setSubject("Test", "UTF-8");
msg.setText("This is a test.", "UTF-8");
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(props.getProperty("test.to"), false));
System.out.println("Sending message...");
Transport.send(msg);
System.out.println("Message was sent.");
} catch (AuthenticationFailedException ex) {
System.err.println("Authentication failed - please check the username and password.");
ex.printStackTrace();
} catch (SMTPSendFailedException ex) {
System.err.println("SMTP error - please check your SMTP settings.");
ex.printStackTrace();
} catch (MessagingException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("Performing mail test.");
Properties props = new Properties();
// SMTP server and port
props.put("mail.smtp.host", "smtp.office365.com");
props.put("mail.smtp.port", "587");
// If set to false, username + password will not be used
props.put("mail.smtp.auth", "true");
props.put("mail.username", "test@example.com");
props.put("mail.password", "yourpassword");
// If true, enables the use of the STARTTLS command (if supported by the server)
// to switch the connection to a TLS-protected connection before issuing any login commands.
// Defaults to false.
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.protocols", "TLSv1.3");
// If true, requires the use of the STARTTLS command. If the server doesn't support the STARTTLS command,
// or the command fails, the connect method will fail. Defaults to false.
props.put("mail.smtp.starttls.required", "true");
// FROM / TO address
props.put("test.from", "test@example.com");
props.put("test.to", "test@example.com");
MailTest test = new MailTest(props);
test.sendMail();
}
}