Java电子邮件异常 - 无法连接到SMTP主机

时间:2012-11-08 19:36:36

标签: java email exception

这个错误让我疯了。当我的应用程序抛出异常时,我正在尝试发送自己的电子邮件,但我刚开始收到此错误。我能够成功ping主机,当我使用nslookup时,我可以获得本地IP地址。有任何想法吗?感谢

        try
    {
        java.util.Date d = new Date();
        Properties props = new Properties();

        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", "My server address");
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.port", "25");

        Authenticator authenticator = new SmtpAuthenticator();

        Session mailSession = Session.getDefaultInstance(props, authenticator);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setFrom(new InternetAddress("My email"));
        message.setSubject("Voicemail Notification");
        message.setContent("You are receiving this automatic message because there has been a voicemail left in the system on " +d.toString() + " for the division of the company you work for. \n" +
        "Please respond promptly.  Thanks!\n" +
        "Private Ip address?file=" + messageName + ".gsm", "text/plain");
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));
        transport.connect();
        transport.sendMessage(message,
                message.getRecipients(Message.RecipientType.TO));
        transport.close();

        System.out.println("Email Sent!");

    }
    catch(Exception ex)
    {
        ex.printStackTrace();
        System.out.println("Unable to send email notification to: " + emailTo);
    }

1 个答案:

答案 0 :(得分:0)

尝试使用命令行尝试telnet mailserveraddress 25这将告诉您邮件服务器是否实际正在运行并在端口25上侦听。如果您在telnet中收到相同的错误,那么这意味着您的邮件服务器不是运行正常。