Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29925

Send email with attachment which contains byte[] via java mail

$
0
0

I have following byte[] which comes from database.

0x255044462D312E330A25AAABAC

Note: above byte array is sample full file is not here because of length.

UPDATE:

But I am getting like [B@7ffd10fa format


  • Before you see code please read here:

When I send bytes which returns getPdfByteStream() method it sends attachment in email like original file. But when I get from database and send it sends corrupted file.


UPDATE:

Entity.class

@Lob
@Column(name = "ATTACHED_FILE")
private byte[] attachedFile;

//getter()/setter();

Code which sends email

 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();
    }

getPDFByteStream() method

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;
}

Can anyone guide me.

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.

Please comment below if you have any query regarding question. Thanks.


Viewing all articles
Browse latest Browse all 29925

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>