【问题标题】:Java SMTP mail - Connection timed out ErrorJava SMTP 邮件 - 连接超时错误
【发布时间】:2013-12-11 18:38:16
【问题描述】:

我一直在开发一个程序来从基于桌面的 Java 应用程序发送电子邮件。

当我使用 Google SMTP 服务器 (smtp.gmail.com) 测试它时,该程序运行良好,但是当我为其他 smtp 服务器测试它时,它会产生错误 -

javax.mail.MessagingException: Could not connect to SMTP host:
 smtp.collaborationhost.net, port: 465;
 nested exception is:
 java.net.ConnectException: Connection timed out: connect

我在 Eclipse 中运行代码。

下面是代码sn-p -

public class Emailer {

    public static void main(String[] args) {

        String name = "Testing";

        Properties props = new Properties();

        props.put("mail.smtp.host", "smtp.collaborationhost.net");
        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("qwert@xyzcompany.com","*******");
                }
        });

        try
        {
            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress("qwert@xyzcompany.com"));
            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("testgmail.com"));     
            message.setSubject("Generic Mail Test from Eclipse !!!");
            message.setText("This is a Test Mail, sent from Local Eclipse System via Google SMTP server. \n\n" + "Regards, \n" + name + "." );
            Transport.send(message);

            System.out.println("Message Sent !!!");
        }

        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

【问题讨论】:

  • 如果完全相同的代码适用于另一个 SMTP 服务器,那么它可能不是代码语法。检查您是否可以从您正在运行的机器、有效的电子邮件帐户、正确的端口等访问...
  • 所有的道具都正确吗?由于它适用于谷歌,我认为你的问题是你有一个错误的端口或类似的东西。

标签: java email smtp jakarta-mail smtpclient


【解决方案1】:

此 JavaMail 常见问题解答条目将帮助您 debug connection problems。在这种情况下,我怀疑其他服务器没有使用与 Gmail 相同的端口,因此您需要更改配置。

此外,您可以通过更正其中的一些common mistakes 来简化程序。

【讨论】:

    猜你喜欢
    • 2017-01-19
    • 2014-11-29
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多