我正在开发一个 Java 应用程序,我正在使用 Javamail 发送邮件。我的代码如下:
Properties props = new Properties();
props.put("mail.smtp.host", "diana.cartif.es");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("alerts","pass");
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("alerts@cartif.es"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("campus21@cartif.es"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +"\n\n No spam to my email, please!");
Transport.send(message);
但是,当我执行此代码时,它会引发异常:
javax.mail.MessagingException: Could not connect to SMTP host: diana.cartif.es, port: 465, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1960)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at com.cartif.data.MainConnection.getFTPConnection(MainConnection.java:106)
at com.cartif.main.Main.connectToServer(Main.java:72)
at com.cartif.main.Main.main(Main.java:60)
要连接的数据是正确的,因为我在我的邮件服务器中检查了它。有人可以帮我吗?
谢谢!