title: Access denied to default session when i try to send mail automatically using scheduler
body: <p>I have a requirement of sending the Email automatically from and to my corporate Email account in specific Intervals of time. But I get <code>Access Denied to default session</code> Exception even though I am not using default instance. Authentication is not required. Please help me solve this exception and send mail in regular intervals</p>
  
  <p><strong>MailServer.java</strong></p>
  
  <pre><code>public class MailServer extends javax.mail.Authenticator
  {
          private String mailhost  ="host";  //"smtp.mail.yahoo.com"; //"smtp.mail.com";
      final String username = "username";
  final String password = "password";
     private Session session;  
  
     public MailServer(String user, String password) {
  
  
      Properties props = new Properties();
      props.put("mail.smtp.auth", "false");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.host", "host");
      props.put("mail.smtp.port", "25");
  
      Session session = Session.getInstance(props,
        new javax.mail.Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(username, username);
          }
        });
  
      session = Session.getInstance(props, this);
  }  
  
  protected PasswordAuthentication getPasswordAuthentication()
  {
      return new PasswordAuthentication(password, password);
  }  
  
  public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception
  {
      MimeMessage message = new MimeMessage(session);
      DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
      message.setSender(new InternetAddress(sender));
      message.setSubject(subject);
      message.setDataHandler(handler);
      if (recipients.indexOf(',') &gt; 0)
  
          message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
  
          else
  
          message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
  
          Transport.send(message);
  }  
  </code></pre>
  
  <p><strong>.properties File</strong></p>
  
  <pre><code>setFrom = fgf.er@xxx.com
  setPassword=xxx
  
  
  #repeat for every 10 seconds
  timetoquery = 10s
  
    #start after 2seconds for first time..
    delay = 2s
  
    emailTO = fgh.gm@xxx.com,fgh.hg@xxx.com
  </code></pre>
  
  <p><strong>Testing.java</strong></p>
  
  <pre><code>public class Testing extends TimerTask
   {
  
     public void run()
     {
  
                 MailServer sender = new MailServer(Constants.setFrom, Constants.setPassword);
  
                 try {
                sender.sendMail("Subject","This is Java4s",Constants.setFrom,Constants.emailTO);
          }
                 catch (Exception e) {
               e.printStackTrace();
          }  
  
              System.out.println("Email Sent Succesfully...");
  
          }
  }
  </code></pre>
  
  <p><strong>CONSOLE</strong></p>
  
  <pre><code>Scheduler started..
  java.lang.SecurityException: Access to default session denied
  at javax.mail.Session.getDefaultInstance(Session.java:292)
  at javax.mail.Transport.send0(Transport.java:141)
  at javax.mail.Transport.send(Transport.java:80)
  at java4s.MailServer.sendMail(MailServer.java:63)
  at java4s.Testing.run(Testing.java:15)
  at java.util.TimerThread.mainLoop(Timer.java:555)
  at java.util.TimerThread.run(Timer.java:505)
  Email Sent Succesfully...
  </code></pre>