Question

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 ?

Was it helpful?

Solution

Try this code:

 MimeBodyPart att = new MimeBodyPart(); 
ByteArrayDataSource bds = new ByteArrayDataSource(bytearray, "AttName"); 
att.setDataHandler(new DataHandler(bds)); 
att.setFileName(bds.getName()); 

OTHER TIPS

Try this code,

DataHandler lDataHandler = new DataHandler(new ByteArrayDataSource(fichierByteVO.getFile(), fichierByteVO.getMIMEType())); 
lMessageBodyPart.setDataHandler(lDataHandler);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top