使用 TSL 通过 SMTP 发送电子邮件
下面发布的代码可以很好地帮助我通过带有 SSL 的 STMP 发送电子邮件。 现在 SMTP 更改为 TSL,我无法用它发送电子邮件。 我尝试了一些方法,例如添加
props.put("mail.smtp.starttls.enable","true");
,但没有用。
错误代码显示:
“javax.mail.MessagingException:无法连接到 SMTP 主机:exch.studi.fhws.de,端口:587; 嵌套异常是: javax.net.ssl.SSLException:无法识别的 SSL 消息,纯文本连接?”
有什么想法吗?
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail
{
String d_email = "...",
password = "...",
host = "...",
port = "25",
mailTo = "...",
mailSubject = "test",
mailText = "test";
public SendMail()
{
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SecurityManager security = System.getSecurityManager();
try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText(mailText);
msg.setSubject(mailSubject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
Transport.send(msg);
}
catch (Exception mex)
{
mex.printStackTrace();
}
}
public static void main(String[] args)
{
TextClass tc = new TextClass();
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(d_email, password);
}
}
}
The code posted below works fine for me to send an email over an STMP with SSL.
Now the SMTP changed to TSL and i dont manage to send email with it.
I tried several things like adding
props.put("mail.smtp.starttls.enable","true");
but it was no use.
The error code says:
"javax.mail.MessagingException: Could not connect to SMTP host: exch.studi.fhws.de, port: 587;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?"
Any ideas?
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail
{
String d_email = "...",
password = "...",
host = "...",
port = "25",
mailTo = "...",
mailSubject = "test",
mailText = "test";
public SendMail()
{
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SecurityManager security = System.getSecurityManager();
try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText(mailText);
msg.setSubject(mailSubject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
Transport.send(msg);
}
catch (Exception mex)
{
mex.printStackTrace();
}
}
public static void main(String[] args)
{
TextClass tc = new TextClass();
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(d_email, password);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误消息表明您正在连接到端口 587 - 这是一个启用 SSL 的电子邮件端口,这意味着连接从一开始就必须是 SSL。您的代码通过纯文本(例如纯端口 25)进行连接,然后尝试启动 TLS。这是行不通的,因为端口 587 期望立即进行 SSL 交换,而不是“EHLO ... / STARTTLS”明文命令集。
The error message says you're connecting to port 587 - this is an SSL-enabled email port, which means the connection has to be SSL from the get-go. Your code is connecting via plaintext (e.g. plain port 25) and THEN attempting to start TLS. This won't work, as port 587 expects an SSL exchange immediately, not a "EHLO ... / STARTTLS" plaintext command set.