title: IO Exception while sending mail using JavaMail (Access is denied )
body: <p>I am trying to send a mail using JavaMail but getting <strong>IO exception</strong> saying <strong>Access is denied</strong>
     Please let me know where i am making a mistake. I have used java mail and internet APIs along with the datasource , datahandler APIs. Also the html file i m trying to send as an attachment is in appropriate path and i have the access rights to that file. Below is my code </p>
  
  <pre><code>import javax.activation.DataHandler;
  import javax.activation.DataSource;
  import javax.activation.FileDataSource;
  import javax.mail.*;
  import javax.mail.internet.*;
  import java.util.*;
  
  public class SendMail
  
  {
  
  public static void execute(String reportFileName) throws Exception
  
  {
      String path="C:/reports";
  
      String[] to={"xyz@gmail.com"};
      String[] cc={"xyz@gmail.com"};
      String[] bcc={"abc@gmail.com"};
  
      SendMail.sendMail("xyz@gmail.com",
                          "******",
                          "smtp.gmail.com",
                          "465",
                          "true",
                          "true",
                           true,
                          "javax.net.ssl.SSLSocketFactory",
                          "false",
                           to,
                           cc,
                           bcc,
                          "Test Mail",
                          "Test Message",
                          path,
                          reportFileName);
    }
  
    public  static boolean sendMail(String userName,
              String passWord,
              String host,
              String port,
              String starttls,
              String auth,
              boolean debug,
              String socketFactoryClass,
              String fallback,
              String[] to,
              String[] cc,
              String[] bcc,
              String subject,
              String text,
              String attachmentPath,
              String attachmentName){
  
      //Object Instantiation of a properties file.
      Properties props = new Properties();
  
      props.put("mail.smtp.user", userName);
  
      props.put("mail.smtp.host", host);
  
      props.put("mail.smtp.password", passWord);
  
      props.put("mail.smtps.auth", "true");
  
  
  
      if(!"".equals(port)){
      props.put("mail.smtp.port", port);
      }
  
      if(!"".equals(starttls)){
          props.put("mail.smtp.starttls.enable",starttls);
          props.put("mail.smtp.auth", auth);
      }
  
      if(debug){
  
      props.put("mail.smtp.debug", "true");
  
      }else{
  
      props.put("mail.smtp.debug", "false");
  
      }
  
      if(!"".equals(port)){
          props.put("mail.smtp.socketFactory.port", port);
      }
      if(!"".equals(socketFactoryClass)){
          props.put("mail.smtp.socketFactory.class",socketFactoryClass);
      }
      if(!"".equals(fallback)){
          props.put("mail.smtp.socketFactory.fallback", fallback);
      }
  
      try{
           props.put("mail.smtp.isSSL", "true");
  
          Session session = Session.getDefaultInstance(props, null);
  
          session.setDebug(debug);
  
          MimeMessage msg = new MimeMessage(session);
  
          msg.setText(text);
  
          msg.setSubject(subject);
  
          Multipart multipart = new MimeMultipart();
          MimeBodyPart messageBodyPart = new MimeBodyPart();
          DataSource source = new FileDataSource(attachmentPath);
          messageBodyPart.setDataHandler(new DataHandler(source));
          messageBodyPart.setFileName(attachmentName);
          multipart.addBodyPart(messageBodyPart);
  
          msg.setContent(multipart);
          msg.setFrom(new InternetAddress(userName));
  
          for(int i=0;i&lt;to.length;i++){
              msg.addRecipient(Message.RecipientType.TO, new
  
        InternetAddress(to[i]));
          }
  
          for(int i=0;i&lt;cc.length;i++){
              msg.addRecipient(Message.RecipientType.CC, new
         InternetAddress(cc[i]));
          }
  
          for(int i=0;i&lt;bcc.length;i++){
              msg.addRecipient(Message.RecipientType.BCC, new
        InternetAddress(bcc[i]));
          }
  
          msg.saveChanges();
  
          Transport transport = session.getTransport("smtps");
  
          transport.connect("smtp.gmail.com","xyz@gmail.com", "******");
  
          transport.sendMessage(msg, msg.getAllRecipients());
  
          transport.close();
  
          return true;
  
      } catch (Exception mex){
          mex.printStackTrace();
          return false;
      }
  }
  
    public static void main (String []args) throws Exception
    {
        SendMail.execute("dummy_report.html");
    }
  }
  </code></pre>
  
  <p>Below is the <strong>StackTrace</strong></p>
  
  <pre><code>    java.io.FileNotFoundException: C:\reports (Access is denied)
  at java.io.FileInputStream.open(Native Method)
  at java.io.FileInputStream.&lt;init&gt;(Unknown Source)
  at javax.activation.FileDataSource.getInputStream(Unknown Source)
  at javax.activation.DataHandler.writeTo(Unknown Source)
  at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
  at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:845)
  at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:361)
  at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:85)
  at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source)
  at javax.activation.DataHandler.writeTo(Unknown Source)
  at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
  at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1683)
  at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:585)
  at sel.SendMail.sendMail(SendMail.java:143)
  at sel.SendMail.execute(SendMail.java:22)
  at sel.SendMail.main(SendMail.java:157)
      javax.mail.MessagingException: IOException while sending message;
        nested exception is:
  java.io.FileNotFoundException: C:\reports (Access is denied)
  at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:625)
  at sel.SendMail.sendMail(SendMail.java:143)
  at sel.SendMail.execute(SendMail.java:22)
  at sel.SendMail.main(SendMail.java:157)
      Caused by: java.io.FileNotFoundException: C:\reports (Access is denied)
  at java.io.FileInputStream.open(Native Method)
  at java.io.FileInputStream.&lt;init&gt;(Unknown Source)
  at javax.activation.FileDataSource.getInputStream(Unknown Source)
  at javax.activation.DataHandler.writeTo(Unknown Source)
  at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
  at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:845)
  at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:361)
  at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:85)
  at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source)
  at javax.activation.DataHandler.writeTo(Unknown Source)
  at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
  at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1683)
  at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:585)
  ... 3 more
  </code></pre>