Playframework,二进制pdf文件附件问题

时间:2012-08-16 12:26:24

标签: email playframework

我有一个作为ByteArray的pdf文件,我想知道是否有办法在不在服务器上创建主文件的情况下附加它。

Play文档提供的代码仅允许附加真实文件。

EmailAttachment attachment = new EmailAttachment();
attachment.setDescription("A pdf document");
attachment.setPath(Play.getFile("rules.pdf").getPath());

我正在使用Playframework Mail模块。

谢谢!

2 个答案:

答案 0 :(得分:2)

由于Play 1.x使用了Apache Commons Email库,您可以使用MultiPartEmail#attach(DataSource ds, String name, String description)方法:

import org.apache.commons.mail.*;

// create the mail
MultiPartEmail email = new MultiPartEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");

// get your inputstream from your db
InputStream is = new BufferedInputStream(MyUtils.getBlob());  
DataSource source = new ByteArrayDataSource(is, "application/pdf");  

// add the attachment
email.attach(source, "somefile.pdf", "Description of some file");

// send the email
email.send();

答案 1 :(得分:1)

即将发布的游戏版本1.3将引入一种方法attachDataSource(),可以在Mailer类中调用。这将允许您轻松附加ByteArray作为电子邮件的附件,而无需先将其保存到磁盘或无需使用Apache Commons电子邮件。然后,您可以使用“标准”播放方式。

以下是play bugtracker中相应的功能请求: http://play.lighthouseapp.com/projects/57987/tickets/1500-adding-maillerattachdatasource-functionality