Java program - IP address ping and send email program

问题: relative newcomer to programming. I am attempting to write a program that pings an IP address. When the host is online it does nothing, but when offline it sends an email t...

问题:

relative newcomer to programming. I am attempting to write a program that pings an IP address. When the host is online it does nothing, but when offline it sends an email to a recipient. It is however sending an email every time - online and offline! I know I'm pretty close, any help would be great! Code snippet...

    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 OFFLINEn");

    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,"
            + "nn 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:

Your message part is not in the scope of your else-statement. if you don't use curly brackets for if / else / for / etc., only the next line will be considered. just wrap it with {}

        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 OFFLINEn");

        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,"
                + "nn 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:

Your try block is not related to the if statement. A good practice in java is always use curly braces for if/else blocks, if a curly braces is not used only the first line of code will be executed regarding the if/else response.

For example, in the following code

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

doSomethingElse() will ALWAYS execute.

and in this code:

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

doSometingElse() will only execute if someTest() is true

  • 发表于 2019-01-18 22:24
  • 阅读 ( 274 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除