【问题标题】:Java program - IP address ping and send email programJava 程序 - IP 地址 ping 和发送电子邮件程序
【发布时间】:2019-06-11 13:21:12
【问题描述】:

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

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

【问题讨论】:

  • 这就是为什么你应该将单行语句放在大括号中的 if/else 语句中!

标签: java ip-address ping


【解决方案1】:

您的消息部分不在您的 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);
        }
    } 
}

【讨论】:

  • 啊!十分感谢大家!非常感谢,我想我已经盯着它太久了。需要休息一下!
  • 欢迎 :),正如您所说,您已经非常接近解决方案了。
【解决方案2】:

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

例如在下面的代码中

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

doSomethingElse() 将始终执行。

在这段代码中:

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

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

【讨论】:

    猜你喜欢
    • 2018-05-10
    • 2014-04-11
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 2012-01-21
    • 2012-02-17
    相关资源
    最近更新 更多