【发布时间】:2011-10-28 05:03:32
【问题描述】:
下面发布的代码适用于我通过带有 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);
}
}
}
【问题讨论】:
-
谢谢,上面的代码是一样的——我用了这个例子。
标签: java email smtp jakarta-mail ssl