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

时间:2021-07-04 18:15:54

This error is driving me nuts. I'm trying to send myself emails when my application throws exceptions but I just started getting this error. I am able to successfully ping the host and I can get a local ip address when I use nslookup. Any ideas? Thanks

这个错误让我疯了。当我的应用程序抛出异常时,我正在尝试发送自己的电子邮件,但我刚开始收到此错误。我能够成功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 个解决方案

#1


0  

Try using command line to try telnet mailserveraddress 25 this will tell you whether or not the mail server is actually running and listening on port 25. If you receive the same error in telnet, then that means your mail server is not running properly.

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

#1


0  

Try using command line to try telnet mailserveraddress 25 this will tell you whether or not the mail server is actually running and listening on port 25. If you receive the same error in telnet, then that means your mail server is not running properly.

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