【问题标题】:Sending mail from java with MS exchange server giving connection timeout [duplicate]使用 MS 交换服务器从 java 发送邮件给出连接超时 [重复]
【发布时间】:2014-09-29 18:15:27
【问题描述】:

我正在尝试从我的邮件服务器主机为 exg6.exghost.com 的 java web-app 发送邮件,但它给出了 Connection timed out 错误。当我的主机是 smtp.gmail.com 时,同样的代码可以工作。

我的代码是

Properties props = new Properties();
        props.put("mail.smtp.host", "exg6.exghost.com");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "false");

        Session session1 = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication(username,password);
                }
            });

        try {

            Message message = new MimeMessage(session1);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("xyz@gmail.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler," +
                    "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

我也尝试将 ma​​il.smtp.auth 设置为 true,但再次连接超时。

请帮忙。

【问题讨论】:

标签: java email jakarta-mail exchange-server


【解决方案1】:

请参阅tips for debugging connection problems 的 JavaMail 常见问题解答。

您似乎正在使用默认(纯文本)SMTP 端口进行连接。您的服务器很可能只接受 SMTP-over-SSL 端口上的连接。添加这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 2020-11-18
    • 2016-01-02
    • 2017-09-30
    • 2019-06-16
    相关资源
    最近更新 更多