Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java电子邮件异常--无法连接到SMTP主机_Java_Email_Exception - Fatal编程技术网

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

Java电子邮件异常--无法连接到SMTP主机,java,email,exception,Java,Email,Exception,这个错误快把我逼疯了。当我的应用程序抛出异常时,我试图给自己发送电子邮件,但我刚开始遇到这个错误。我能够成功ping主机,并且在使用nslookup时可以获得本地ip地址。有什么想法吗?谢谢 try { java.util.Date d = new Date(); Properties props = new Properties(); props.setProperty("mail.transport.protocol"

这个错误快把我逼疯了。当我的应用程序抛出异常时,我试图给自己发送电子邮件,但我刚开始遇到这个错误。我能够成功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);
    }

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

您可以使用telnet连接到
telnet邮件服务器25
?@MitchDempsey,您应该添加您的评论作为答案,我将接受它。我犯了一个愚蠢的错误。突然发问,但你的评论起了作用。谢谢