title: Check for a condition and send mail in java
body: <p>i need to send mail in java by </p>
  
  <ol>
  <li>checking a folder</li>
  <li>if there is no file in that folder</li>
  <li>send a mail saying folder is empty</li>
  </ol>
  
  <p>i have tried a code for sending a mail using a schedular to send a mail every 2 seconds and stop it after 2 seconds but unaware how to check the condition
  i have three classes mail sender,main and scheduled task</p>
  
  <p>Can anyone please help??</p>
  
  <h2>Thanks in advance!!!!</h2>
  
  <p>MailSender.java</p>
  
  <pre><code>public class MailSender {
  
      public static void send(String string, String messageString, String msgSubject) throws Exception {
  
        // Sender's email ID needs to be mentioned
        String from = "xyz@xyz.com";
  
        // Assuming you are sending email from localhost
        String host = "localhost";
  
        String port = "25";
  
        // Get system properties
        Properties properties = System.getProperties();
  
        // Setup mail server
        properties.setProperty("mail.smtp.host", host);
        properties.setProperty("mail.smtp.port", port);      
  
        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties);
  
        try{
           // Create a default MimeMessage object.
           MimeMessage message = new MimeMessage(session);
  
           // Set From: header field of the header.
           message.setFrom(new InternetAddress(from));
  
           // Set To: header field of the header.
           message.addRecipient(Message.RecipientType.TO, new InternetAddress(string));
  
           // Set Subject: header field
           message.setSubject(msgSubject);
  
           // Create the message part 
           BodyPart messageBodyPart = new MimeBodyPart();
  
           // Fill the message
           messageBodyPart.setText(messageString);
           messageBodyPart.setFileName("xyz.png");
  
           // Create a multi-part message
           Multipart multipart = new MimeMultipart();
  
           // Set text message part
           multipart.addBodyPart(messageBodyPart);
           message.setContent(messageString, "text/html; charset=utf-8");
  
          MimeMessage message1 = new MimeMessage(session);
          message1.setContent
          ("&lt;h1&gt;This is a test&lt;/h1&gt;" 
           + "&lt;/images/zyx.png\"&gt;", 
           "text/html");
  
           // Send message
           Transport.send(message);
        }catch (MessagingException mex) {
           mex.printStackTrace();
        }
     }
  </code></pre>
  
  <p>MainClass.java</p>
  
  <pre><code>public class MainClass {
      public static void main(String args[]) throws Exception {
  
          Timer time = new Timer(); // Instantiate Timer Object
          ScheduledTask st = new ScheduledTask(); // Instantiate SheduledTask class
          time.schedule(st, 0, 10000); // Create Repetitively task for every 1 secs
  
          //for demo only.
          for (int i = 0; i &lt;= 2; i++) {
              MailSender.send("xyz@asd.com", "Welcome to abc", "Welcom eMail");
              Thread.sleep(2000);
              if (i == 2) {
                  System.out.println("Application Terminates");
                  System.exit(0);
              }
          }
      }
  }
  </code></pre>
  
  <p>ScheduledTask.java</p>
  
  <pre><code>public class ScheduledTask extends TimerTask {
  
      Date now; // to display current time
  
      // Add your task here
      public void run() {
          now = new Date(); // initialize date
          System.out.println("Time is :" + now); // Display current time
      }
  }
  </code></pre>