我有一个名为 lSession 的 javax.mail.Session 和一个 MimeMessage lMessage :
Session lSession = Session.getDefaultInstance(properties);
MimeMessage lMessage = new MimeMessage(lSession);
我得到了一个包含文件表示的字节数组列表:
List <byte[]> pPiecesJointes
我尝试将这些文件附加到消息中,但无法修复它....
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);
如果有人知道谁用字节数组附加文件?
试试这个代码:
MimeBodyPart att = new MimeBodyPart();
ByteArrayDataSource bds = new ByteArrayDataSource(bytearray, "application/octet-stream");
att.setDataHandler(new DataHandler(bds));
att.setFileName("AttachmentFileNameWithExt");
试试这个代码,
DataHandler lDataHandler = new DataHandler(new ByteArrayDataSource(fichierByteVO.getFile(), fichierByteVO.getMIMEType()));
lMessageBodyPart.setDataHandler(lDataHandler);