title: How to send the mail with attachment in spring
body: <p>when is trying to send the mail using zip mime type is getting some random format attachment. so tried to change the mime type by hard coding, need to know how to get the mime type from bytearraysource object</p>
  
  <pre><code>package com.mail.send;
  
  import java.io.ByteArrayOutputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.util.zip.ZipEntry;
  import java.util.zip.ZipOutputStream;
  
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
  public class MailImpl {
  public static void main(String args[]) throws IOException{
  
      ApplicationContext context=new ClassPathXmlApplicationContext("Spring.xml");
      JavaMail javaMail=(JavaMail) context.getBean("JavaMail");
      System.out.println("after bean creation");
      String to="abc@gmail.com";
      String cc="xyz@gmail.com";
      String subject="Pdf notification";
      String content="PDF has been generated";
      boolean flag = true ;
      File file =new File("E:\\Sample.pdf");
  
      //byte [] sample=null;
  
      /*start of modify*/
       FileInputStream fin = null;
       byte fileContent[] = new byte[(int)file.length()];
      try {
          // create FileInputStream object
          fin = new FileInputStream(file);
  
  
  
          // Reads up to certain bytes of data from this input stream into an array of bytes.
          fin.read(fileContent);
          //create string from byte array
          String s = new String(fileContent);
          System.out.println("File content: " + s);
      }
      catch (FileNotFoundException e) {
          System.out.println("File not found" + e);
      }
      catch (IOException ioe) {
          System.out.println("Exception while reading file " + ioe);
      }
      finally {
          // close the streams using close method
          try {
              if (fin != null) {
                  fin.close();
              }
          }
          catch (IOException ioe) {
              System.out.println("Error while closing stream: " + ioe);
          }
      }
      byte[] fileContent1 = null;
  
      /*end of modify */
      if(flag==true){
          fileContent1=zipBytes("Sample.pdf", fileContent);//document.getmimetype
      }else{
          fileContent1 = fileContent;
      }
      javaMail.sendEmail(to, cc, subject, content, fileContent1);
  
  
  }
  
  public static byte[] zipBytes(String filename, byte[] input) throws IOException {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ZipOutputStream zos = new ZipOutputStream(baos);
      ZipEntry entry = new ZipEntry(filename);
      entry.setSize(input.length);
      zos.putNextEntry(entry);
      zos.write(input);
      zos.closeEntry();
      zos.close();
  
  
  
      return baos.toByteArray();
  }
  
  
  }
  </code></pre>