Java 程序 - IP 地址 ping 和发送电子邮件程序

Java program - IP address ping and send email program

编程新手。我正在尝试编写一个程序来 ping 一个 IP 地址。当主机在线时,它什么都不做,但当它离线时,它会向收件人发送电子邮件。然而,它每次都会发送一封电子邮件 - 在线和离线!我知道我很接近,任何帮助都会很棒! 代码片段...

    final String username = "myemail@gmail.com";
    final String password = "Password";    

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });    


    InetAddress i = InetAddress.getByName(ipAddress);
    System.out.println("Sending Ping Request to " + ipAddress);
    if (i.isReachable(5000)) //5 second limit
        System.out.println("Host is online \n");
    else
        System.out.println("HOST IS OFFLINE\n");

    try {


        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("myemail@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("receiveremail@hotmail.com"));
        message.setSubject("Project Test Mail");
        message.setText("Test Mail,"
            + "\n\n Sent From sendMail.java application");

        Transport.send(message);

        System.out.println("Mail Sent to receiveremail@hotmail.com " 
         +     ipAddress);

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

您的消息部分不在您的 else 语句的范围内。 如果您不对 if / else / for / 等使用大括号,则只考虑下一行。 只需将其放入 {}

        final String username = "myemail@gmail.com";
        final String password = "Password";    

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });    


        InetAddress i = InetAddress.getByName(ipAddress);
        System.out.println("Sending Ping Request to " + ipAddress);
        if (i.isReachable(5000)) //5 second limit
            System.out.println("Host is online \n");
        else {
            System.out.println("HOST IS OFFLINE\n");

        try {


            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("myemail@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("receiveremail@hotmail.com"));
            message.setSubject("Project Test Mail");
            message.setText("Test Mail,"
                + "\n\n Sent From sendMail.java application");

            Transport.send(message);

            System.out.println("Mail Sent to receiveremail@hotmail.com " 
             +     ipAddress);

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

您的 try 块与 if 语句无关。 java 中的一个好习惯是始终对 if/else 块使用大括号,如果不使用大括号,则只会执行关于 if/else 响应的第一行代码。

例如,在下面的代码中

if(someTest())
  doSomething();
  doSomethingElse();

doSomethingElse() 将始终执行。

在此代码中:

if(someTest()){
  doSomething();
  doSomethingElse();
 }

doSometingElse() 只会在 someTest() 为真时执行