title: Send email with attachment which contains byte[] via java mail
body: <p>I have following <code>byte[]</code> which comes from database.</p>
  
  <pre><code>0x255044462D312E330A25AAABAC
  </code></pre>
  
  <p><strong>Note: above byte array is sample full file is not here because of length.</strong> </p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>But I am getting like <code>[B@7ffd10fa</code> format</p>
  
  <hr>
  
  <ul>
  <li>Before you see code please read here:</li>
  </ul>
  
  <p>When I send <code>bytes</code> which returns <code>getPdfByteStream()</code> method it sends attachment in email like original file. But when I get from database and send it sends corrupted file.</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p><em>Entity.class</em></p>
  
  <pre><code>@Lob
  @Column(name = "ATTACHED_FILE")
  private byte[] attachedFile;
  
  //getter()/setter();
  </code></pre>
  
  <p><strong>Code which sends email</strong></p>
  
  <pre><code> try {
          MimeBodyPart textBodyPart = new MimeBodyPart();
          textBodyPart.setText(content);
  
         //byte[] bytes = getPDFByteStream(); //Returns byte[] reading local drive file
  
  
           **UPDATE:**
  
          //bytes[] bytes=entity.getAttachedFile(); // It gets value from entity.
  
          /**
          ** It is getting like "[B@7ffd10fa" format but m storing on database like "0x255044462D312E330A25" format
          **/
  
          String string="0x255044462D312E330A25";
          byte[] bytes =string.getBytes(Charset.forName("UTF-8"));
          System.out.println("bytes " + bytes.toString());
  
          DataSource dataSource = new ByteArrayDataSource(bytes, "application/pdf");
          MimeBodyPart pdfBodyPart = new MimeBodyPart();
          pdfBodyPart.setDataHandler(new DataHandler(dataSource));
          pdfBodyPart.setFileName("bankAdminReport.pdf");
  
          MimeMultipart mimeMultipart = new MimeMultipart();
          mimeMultipart.addBodyPart(textBodyPart);
          mimeMultipart.addBodyPart(pdfBodyPart);
  
          InternetAddress iaSender = new InternetAddress(sender);
          InternetAddress iaRecipient = new InternetAddress(recipient);
  
          MimeMessage mimeMessage = new MimeMessage(session);
          mimeMessage.setSender(iaSender);
          mimeMessage.setSubject(subject);
          mimeMessage.setRecipient(Message.RecipientType.TO, iaRecipient);
          mimeMessage.setContent(mimeMultipart);
  
          Transport.send(mimeMessage);
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  </code></pre>
  
  <p><strong>getPDFByteStream() method</strong></p>
  
  <pre><code>public static byte[] getPDFByteStream() throws IOException {
      File file = new File("C:\\pdf\\bankAdminReport.pdf");
  
      byte[] b = new byte[(int) file.length()];
      try {
          FileInputStream fileInputStream = new FileInputStream(file);
          fileInputStream.read(b);
      } catch (FileNotFoundException e) {
          System.out.println("File Not Found.");
          e.printStackTrace();
      } catch (IOException e1) {
          System.out.println("Error Reading The File.");
          e1.printStackTrace();
      }
      return b;
  }
  </code></pre>
  
  <p>Can anyone guide me.</p>
  
  <blockquote>
    <p>The main problem is when I send file reading from local drive it sends perfectly. but if I send getting from database or any local variable file corrupts.</p>
  </blockquote>
  
  <p>Please comment below if you have any query regarding question.
  Thanks. </p>