Search code examples
javaip-addressping

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

Solution

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

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