0

我正在开发一个 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)

要连接的数据是正确的,因为我在我的邮件服务器中检查了它。有人可以帮我吗?

谢谢!

4

3 回答 3

0

尝试可能是这样的:

     String  d_email = "email",
             d_password = "pass";

    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.server");
    props.put("mail.smtp.user", d_email);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", port);

    Authenticator auth = new SMTPAuthenticator();
    Session session = Session.getInstance(props, auth);
于 2012-06-05T12:49:28.847 回答
0

摆脱 socketFactory* 属性。在大多数情况下,它们不再需要并添加

props.put("mail.smtp.ssl.enable", true);

尝试调用:

session.setDebug(true);

看看你是否有任何帮助。

如果您使用的是自签名证书,请查看:http ://www.oracle.com/technetwork/java/javamail145sslnotes-1562622.html

于 2012-06-06T04:37:05.527 回答
0

最后,我用以下属性解决了这个问题:

props.setProperty("mail.smtp.host", "smpt.host");
props.setProperty("mail.smtp.starttls.enable", "false");
props.setProperty("mail.smtp.port","25");
props.setProperty("mail.smtp.user", "alerts");
props.setProperty("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props);
session.setDebug(true);

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("mail1@mail.es"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("mail2@mail.es"));
message.setSubject("Subject");
message.setText("Text");

Transport t = session.getTransport("smtp");
t.connect("user","pass");
t.sendMessage(message,message.getAllRecipients());
t.close();
于 2013-02-20T07:09:53.837 回答