2013-08-06 32 views
1

我尝试将一个现成的PDF文件用java邮件,所以我已经尝试下面如何安装Pdf文档到邮件

String filename = "file.pdf"; 

ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
??.write(bos); 

DataSource fds = new ByteArrayDataSource(bos.toByteArray(), "application/pdf"); 
MimeBodyPart mbp2 = new MimeBodyPart();    
mbp2.setDataHandler(new DataHandler(fds)); 
mbp2.setFileName(filename); 

我OT理解将是不是“什么?? ”。 所以请给我建议。

回答

2

javax.mail.util.ByteArrayDataSource中的JavaMail 1.4中引入和下面是在同一

一些指针210

如果你使用Spring的JavaMail API,你可以很容易地完成这种事情(或者至少,就像JavaMail API允许的那样简单)。

附件数据可以是Spring的任何资源抽象,ByteArrayResource只是其中之一。

请注意,Spring API的这一部分是独立存在的,它并不要求(但从中受益)Spring容器。

JavaMailSenderImpl mailSender = ... instantiate and configure JavaMailSenderImpl here 
final byte[] data = .... this holds my PDF data 

mailSender.send(new MimeMessagePreparator() { 
    public void prepare(MimeMessage mimeMessage) throws Exception { 
     MimeMessageHelper helper = new MimeMessageHelper(mimeMessage); 
    // set from, to, subject using helper 
    helper.addAttachment("my.pdf", new ByteArrayResource(data)); 
    } 
}); 
0

请参考下面的代码:

if (arrayInputStream != null && arrayInputStream instanceof ByteArrayInputStream) { 
    // create the second message part with the attachment from a OutputStrean 
    MimeBodyPart attachment= new MimeBodyPart(); 
    ByteArrayDataSource ds = new ByteArrayDataSource(arrayInputStream, "application/pdf"); 
    attachment.setDataHandler(new DataHandler(ds)); 
    attachment.setFileName("Report.pdf"); 
    mimeMultipart.addBodyPart(attachment); 
} 
+0

但是arrayInputStream怎么样? – AloNE

+0

你可以参考这里:http://stackoverflow.com/questions/1502635/send-an-e-mail-with-attachment-using-java-mail-api-without-storing-in-local-mach –

+0

我我还没有得到,在arrayInputStream什么商店 – AloNE