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

时间:2019-01-17 10:52:06

标签: java ip-address ping

相对较新的编程方法。我正在尝试编写可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);
    }
} 

2 个答案:

答案 0 :(得分:0)

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

        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);
        }
    } 
}

答案 1 :(得分:0)

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

例如,在以下代码中

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

doSomethingElse()将始终执行。

以及以下代码:

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

doSometingElse()仅在someTest()为true时执行