import org.apache.commons.cli.*;

import javax.mail.AuthenticationFailedException;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import java.util.Properties;

public class Main {

    private static String appName = "malooer";

    /**
     * Required parameters:
     * -port <PORT_NUMBER>
     * -host <HOST>
     * -user <USER>
     * -pwd <PASSWORD>
     *
     * @param args
     */
    public static void main(String[] args) {
        HelpFormatter hFormatter = new HelpFormatter();
        CommandLine cl;

        if (args.length < 1) {
            hFormatter.printHelp(appName, getOptions());
            return;
        }

        try {
            cl = getCommandline(args);
        } catch (ParseException e) {
            hFormatter.printHelp(appName, getOptions());
            return;
        }

        Integer port = Integer.valueOf(cl.getOptionValue("port"));
        String host = cl.getOptionValue("host");
        String user = cl.getOptionValue("user");
        String pwd = cl.getOptionValue("pwd");

        try {
            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");

            Session session = Session.getInstance(props);

            Transport transport = session.getTransport("smtp");
            transport.connect(host, port, user, pwd);
            transport.close();
            System.out.println("Connection established [" + host + "]");
        } catch (AuthenticationFailedException e) {
            System.out.println("Authentication failed");
        } catch (MessagingException e) {
            System.out.println("Unable to connect to server");
        }
    }

    private static CommandLine getCommandline(String[] args) throws ParseException {
        Options options = getOptions();
        CommandLineParser clParser = new DefaultParser();

        return clParser.parse(options, args);
    }

    private static Options getOptions() {
        Options options = new Options();

        Option portOption = new Option("port", true, "port of server");
        Option hostOption = new Option("host", true, "the host");
        Option userOption = new Option("user", true, "the user");
        Option passOption = new Option("pwd", true, "the password");

        options.addOption(portOption);
        options.addOption(hostOption);
        options.addOption(userOption);
        options.addOption(passOption);

        return options;
    }
}
@@ -49,160 +49,219 @@
 ail.
-AuthenticationFailedException;%0Aimport javax.mail.MessagingException;%0Aimport javax.mail.Session;%0Aimport javax.mail.Transport;%0Aimport java.util.Properties
+*;%0Aimport java.sql.Timestamp;%0Aimport java.util.Date;%0Aimport java.util.Properties;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.ScheduledExecutorService;%0Aimport java.util.concurrent.TimeUnit
 ;%0A%0Ap
@@ -919,16 +919,22 @@
         
+final 
 Integer 
@@ -988,24 +988,30 @@
 ));%0A        
+final 
 String host 
@@ -1043,24 +1043,30 @@
 %22);%0A        
+final 
 String user 
@@ -1102,16 +1102,22 @@
         
+final 
 String p
@@ -1148,39 +1148,25 @@
 pwd%22);%0A%0A
-        try %7B%0A 
+%0A
         
    Prope
@@ -1157,19 +1157,16 @@
         
-   
 Properti
@@ -1202,20 +1202,16 @@
         
-   
- 
 props.pu
@@ -1236,28 +1236,24 @@
 , %22true%22);%0A%0A
-    
         Sess
@@ -1303,19 +1303,39 @@
         
-   
+try %7B%0A            final
  Transpo
@@ -1375,24 +1375,264 @@
 rt(%22smtp%22);%0A
+            Runnable runnable = new Runnable() %7B%0A                public void run() %7B%0A                    Date date = new Date();%0A                    Timestamp timestamp = new Timestamp(date.getTime());%0A                    try %7B%0A            
             
@@ -1673,16 +1673,28 @@
 , pwd);%0A
+            
         
@@ -1720,32 +1720,44 @@
 ();%0A            
+  
+          
 System.out.print
@@ -1759,17 +1759,36 @@
 println(
-%22
+timestamp + %22: %5BOK%5D 
 Connecti
@@ -1806,17 +1806,18 @@
 hed 
-%5B
+- 
 %22 + host
  + %22
@@ -1816,17 +1816,23 @@
 host
- + %22%5D%22);%0A
+);%0A            
     
@@ -1871,32 +1871,44 @@
 dException e) %7B%0A
+            
             Syst
@@ -1922,17 +1922,38 @@
 println(
-%22
+timestamp + %22: %5BFAIL%5D 
 Authenti
@@ -1965,24 +1965,36 @@
 n failed%22);%0A
+            
         %7D ca
@@ -2026,32 +2026,44 @@
 ) %7B%0A            
+    
+        
 System.out.print
@@ -2065,17 +2065,38 @@
 println(
-%22
+timestamp + %22: %5BFAIL%5D 
 Unable t
@@ -2110,32 +2110,374 @@
 ct to server%22);%0A
+                    %7D%0A                %7D%0A            %7D;%0A%0A            ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();%0A            service.scheduleAtFixedRate(runnable, 0, 5, TimeUnit.MINUTES);%0A        %7D catch (NoSuchProviderException e) %7B%0A            System.out.println(%22No such provider%22);%0A            return;%0A
         %7D%0A    %7D%0A

Test the connection every 5 minutes and log the result