javaarraysemailbyte

Mail Attachments with byte array


I got a javax.mail.Session named lSession, and a MimeMessage lMessage :

Session lSession = Session.getDefaultInstance(properties);
MimeMessage lMessage = new MimeMessage(lSession);

I got a List of Byte Array who contains file's representations :

List <byte[]> pPiecesJointes

I try to attach these file to the message, but I can't fix it....

if(!pPiecesJointes.isEmpty()){
    lMultipart = new MimeMultipart();
    lMessageBodyPart = new MimeBodyPart();
    // text message
    lMessageBodyPart.setText(pMessage);
    lMultipart.addBodyPart(lMessageBodyPart);
    for(int i = 0; i < pPiecesJointes.size(); i++){
        lMessageBodyPart = new MimeBodyPart();
        /* ?????? How add attachment in lMessageBodyPart with a Byte Array ?
        */ 
        lMultipart.addBodyPart(lMessageBodyPart);
    }
    lMessage.setContent(lMultipart);
}

Transport.send(lMessage);

Please, if somebody knows who attach the file with a byte array ?


Solution

  • Try this code:

    MimeBodyPart att = new MimeBodyPart(); 
    ByteArrayDataSource bds = new ByteArrayDataSource(bytearray, "application/octet-stream"); 
    att.setDataHandler(new DataHandler(bds)); 
    att.setFileName("AttachmentFileNameWithExt");