【发布时间】:2023-03-18 23:25:02
【问题描述】:
我有这段代码可以使用 SSL 和公司邮件服务器发送邮件。
public static void Send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message) throws AddressException, MessagingException {
System.setProperty("java.net.useSystemProxies", "true");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.smtps.starttls.enable", "true");
props.setProperty("mail.smtps.host", "mail.company.au");
props.setProperty("mail.smtps.user", username);
props.setProperty("mail.smtps.password", password);
props.setProperty("mail.smtps.port", "587");
props.setProperty("mail.smtps.auth", "true");
props.setProperty("mail.smtps.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtps.timeout", "5000");
props.setProperty("mail.smtps.connectiontimeout", "5000");
props.setProperty("mail.smtps.writetimeout", "5000");
Session session = Session.getInstance(props,null);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(username));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
if (ccEmail.length() > 0) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
}
msg.setSubject(title);
msg.setText(message, "utf-8");
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport)session.getTransport("smtps");
t.connect("mail.company.au", username, password);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
}
但是我得到了这个错误,有人可以帮忙吗?
调试 SMTP:异常读取响应:javax.net.ssl.SSLException: 无法识别的 SSL 消息,明文连接?
【问题讨论】:
-
你不需要改用
SMTPSSLTransport吗? -
@negacao 代码来自这里:stackoverflow.com/a/3649148/9130545,它使用 SMTPTransport