问题:
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