title: Java send email with file in attachment
body: <p>i want to send a file in attachment using java.
  I have two classes, one where the file location is specified and the second class is used as utility class to send the email
  So when i execute the first class it does not send the email.</p>
  
  <p>First class:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class SendFile {
      private static String[] args;
  
      public static void sendEmail(File filetosend) throws IOException, Exception{
  
      //public static void main(String[] args) throws IOException {
  
      final String username = "email0@gmail.com";
      final String password = "password";
  
      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");
  
      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("email0@gmail.com"));
          message.setRecipients(Message.RecipientType.TO,
                  InternetAddress.parse("email0@gmail.com"));
          message.setSubject("Attach file Test from Netbeans");
          message.setText("PFA");
  
          MimeBodyPart messageBodyPart = new MimeBodyPart();
  
          Multipart multipart = new MimeMultipart();
  
          messageBodyPart = new MimeBodyPart();
  
          //String filetosend = ("c:\\file.txt");
  
          DataSource source = new FileDataSource(filetosend);
          System.out.println("The filetosend is ="+filetosend);
  
          messageBodyPart.setDataHandler(new DataHandler(source));
          System.out.println("The source is ="+source);
  
          messageBodyPart.attachFile(filetosend);
          System.out.println("The file name is ="+messageBodyPart.getFileName());
  
          multipart.addBodyPart(messageBodyPart);
          System.out.println("The message body part is ="+messageBodyPart);
  
          message.setContent(multipart);
          System.out.println("The message multi part is ="+multipart);
  
  
          System.out.println("Sending");
  
          Transport.send(message);
          System.out.println("The message is ="+message);
  
          System.out.println("Done");
  
      } catch (MessagingException e) {
          e.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p>And the second class:</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.io.File;
  import java.io.IOException;
  
  public class Test {
  
      public static void main(String[] args) throws Exception {
  
      }
      File file;
      public void Test() throws IOException, Exception{ 
          System.out.println("Sending the file...");
          File filetosend = new File("c:\\file.txt");
          SendFile.sendEmail(filetosend);
      }
  }
  </code></pre>
  
  <p>There is no error but the file is not sent.
  Any help please, thank you</p>