Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.Properties;
- import java.util.concurrent.TimeUnit;
- import javax.mail.*;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- class URLUtils1 {
- String Url = "http://www.gaoogle.com/";
- void checkIfURLExists() throws InterruptedException {
- HttpURLConnection httpUrlConn;
- try {
- httpUrlConn = (HttpURLConnection) new URL(Url)
- .openConnection();
- httpUrlConn.setRequestMethod("HEAD");
- httpUrlConn.setConnectTimeout(30000);
- httpUrlConn.setReadTimeout(30000);
- if (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK)
- {
- System.out.println("Working");
- recall();
- }
- } catch (IOException | InterruptedException e) {
- System.out.println("Error: " + e.getMessage());
- System.out.println("Not Working");
- recallondead();
- }
- }
- void recall() throws InterruptedException {
- TimeUnit.MINUTES.sleep(1);
- checkIfURLExists();
- }
- void recallondead() throws InterruptedException {
- final String username = "senderxxx@gmail.com";
- final String password = "senderpass";
- 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");
- props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
- Session session = Session.getInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- try {
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress("sender@gmail.com"));
- message.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse("reciever@gmail.com"));
- message.setSubject("Website "+Url+" Is Down");
- message.setText("Dear Sir,"
- + "nn Website "+Url+" Is down");
- Transport.send(message);
- System.out.println("Error..! Mail Sent");
- } catch (MessagingException e) {
- throw new RuntimeException(e);
- }
- TimeUnit.MINUTES.sleep(5);
- checkIfURLExists();
- }
- }
- class URLUtils
- {
- public static void main(String[] args) throws InterruptedException {
- URLUtils1 ab = new URLUtils1();
- ab.checkIfURLExists();
- }
- }
Add Comment
Please, Sign In to add comment