Mail Attachments with byte array

20,484

Solution 1

Try this code:

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

Solution 2

Try this code,

DataHandler lDataHandler = new DataHandler(new ByteArrayDataSource(fichierByteVO.getFile(), fichierByteVO.getMIMEType())); 
lMessageBodyPart.setDataHandler(lDataHandler);
Share:
20,484
user3469203
Author by

user3469203

Updated on July 25, 2020

Comments

  • user3469203
    user3469203 almost 4 years

    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 ?