Java 带有字节数组的邮件附件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23083574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mail Attachments with byte array
提问by user3469203
I got a javax.mail.Session named lSession, and a MimeMessage lMessage :
我有一个名为 lSession 的 javax.mail.Session 和一个 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 ?
请问,如果有人知道谁用字节数组附加文件?
采纳答案by kAnNaN
Try this code:
试试这个代码:
MimeBodyPart att = new MimeBodyPart();
ByteArrayDataSource bds = new ByteArrayDataSource(bytearray, "AttName");
att.setDataHandler(new DataHandler(bds));
att.setFileName(bds.getName());
回答by user3469203
Try this code,
试试这个代码,
DataHandler lDataHandler = new DataHandler(new ByteArrayDataSource(fichierByteVO.getFile(), fichierByteVO.getMIMEType()));
lMessageBodyPart.setDataHandler(lDataHandler);