Guest User

Untitled

a guest
Jun 8th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.HttpURLConnection;
  3. import java.net.URL;
  4. import java.util.Properties;
  5. import java.util.concurrent.TimeUnit;
  6. import javax.mail.*;
  7. import javax.mail.internet.InternetAddress;
  8. import javax.mail.internet.MimeMessage;
  9.  
  10. class URLUtils1 {
  11. String Url = "http://www.gaoogle.com/";
  12. void checkIfURLExists() throws InterruptedException {
  13. HttpURLConnection httpUrlConn;
  14. try {
  15. httpUrlConn = (HttpURLConnection) new URL(Url)
  16. .openConnection();
  17. httpUrlConn.setRequestMethod("HEAD");
  18.  
  19.  
  20. httpUrlConn.setConnectTimeout(30000);
  21. httpUrlConn.setReadTimeout(30000);
  22.  
  23.  
  24.  
  25. if (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK)
  26. {
  27. System.out.println("Working");
  28.  
  29. recall();
  30. }
  31. } catch (IOException | InterruptedException e) {
  32. System.out.println("Error: " + e.getMessage());
  33. System.out.println("Not Working");
  34. recallondead();
  35. }
  36. }
  37.  
  38. void recall() throws InterruptedException {
  39. TimeUnit.MINUTES.sleep(1);
  40. checkIfURLExists();
  41. }
  42.  
  43.  
  44. void recallondead() throws InterruptedException {
  45.  
  46.  
  47.  
  48.  
  49. final String username = "senderxxx@gmail.com";
  50. final String password = "senderpass";
  51.  
  52. Properties props = new Properties();
  53. props.put("mail.smtp.auth", "true");
  54. props.put("mail.smtp.starttls.enable", "true");
  55. props.put("mail.smtp.host", "smtp.gmail.com");
  56. props.put("mail.smtp.port", "587");
  57. props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
  58.  
  59.  
  60. Session session = Session.getInstance(props,
  61. new javax.mail.Authenticator() {
  62. protected PasswordAuthentication getPasswordAuthentication() {
  63. return new PasswordAuthentication(username, password);
  64. }
  65. });
  66.  
  67. try {
  68.  
  69. Message message = new MimeMessage(session);
  70. message.setFrom(new InternetAddress("sender@gmail.com"));
  71. message.setRecipients(Message.RecipientType.TO,
  72. InternetAddress.parse("reciever@gmail.com"));
  73. message.setSubject("Website "+Url+" Is Down");
  74. message.setText("Dear Sir,"
  75. + "nn Website "+Url+" Is down");
  76.  
  77. Transport.send(message);
  78.  
  79. System.out.println("Error..! Mail Sent");
  80.  
  81. } catch (MessagingException e) {
  82. throw new RuntimeException(e);
  83. }
  84.  
  85.  
  86. TimeUnit.MINUTES.sleep(5);
  87. checkIfURLExists();
  88. }
  89.  
  90. }
  91.  
  92. class URLUtils
  93. {
  94. public static void main(String[] args) throws InterruptedException {
  95. URLUtils1 ab = new URLUtils1();
  96.  
  97. ab.checkIfURLExists();
  98. }
  99. }
Add Comment
Please, Sign In to add comment