通过java邮件发送包含byte []的附件的电子邮件

时间:2014-06-03 09:45:15

标签: java email bytearray javamail

我跟随来自数据库的byte[]

0x255044462D312E330A25AAABAC

注意:上面的字节数组是示例完整文件,因为长度不在这里。

更新

但我变得像[B@7ffd10fa格式


  • 在您看到代码之前,请阅读:

当我发送返回bytes方法的getPdfByteStream()时,它会像原始文件一样在电子邮件中发送附件。但是当我从数据库获取并发送它时会发送损坏的文件。


更新

Entity.class

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

//getter()/setter();

发送电子邮件的代码

 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()方法

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

任何人都可以指导我。

  

主要问题是当我从本地驱动器发送文件读取时,它发送完美。但是如果我从数据库或任何本地变量文件发送的内容都会损坏。

如果您对问题有任何疑问,请在下方发表评论。 谢谢。

2 个答案:

答案 0 :(得分:0)

byte[] bytes="0x255044462D312E330A25AAABAC".getBytes();

还将编码前导" 0x"。此外,您似乎正在尝试将十六进制值转换为字节数组,而此方法会将字符值转换为字节。

我相信你要找的是

byte[] bytes = java.xml.bind.DatatypeConverter.parseHexBinary("255044462D312E330A25AAABAC");

试试。

答案 1 :(得分:0)

Imo,这是字符串转换的问题...... 以下示例说明了一种方式 - 可能不是最聪明的:-D

public class TestString2Binary {
    public static void main(String[] args) {
        String testText="This is \n a sample";
        System.out.println("String.toString(): ");
        System.out.println(testText);
        byte[] b=testText.getBytes();
        System.out.println("byte[]-toString(): ");
        System.out.println(b);

        System.out.println("byte[] values - toString(): ");
        for (byte x:b) {
            if (x<100)
                System.out.print("0"+x);
            else
                System.out.print(x);
        }

        String s="084104105115032105115032010032097032115097109112108101";
        System.out.println("imo the back converting to String goes wrong:");
        System.out.println(s.getBytes());
        System.out.println(new String(s.getBytes()));
        System.out.println(s.getBytes(Charset.forName("UTF-8")));
        System.out.println(new String(s.getBytes(Charset.forName("UTF-8"))));

        int recoveredBLength=s.length()/3;
        byte[] recoveredB=new byte[recoveredBLength];
        for (int i=0;i<recoveredBLength;i++) {
            String part=s.substring(i*3,(i*3)+3);
            recoveredB[i]=Byte.parseByte(part);
        }

        System.out.println("the original string: ");
        System.out.println(new String(recoveredB));


    }
}