【问题标题】:add attachment as stream in commons email在公共电子邮件中添加附件作为流
【发布时间】:2011-10-11 23:51:24
【问题描述】:

我在我的网络应用程序中使用Apache Commons Email,它工作正常。

现在我需要通过附件发送文档,我遇到了一些问题。我需要从数据库中获取文件(作为 BLOB)并将其添加为附件。似乎 Commons Email 不支持流附件,它只从路径中获取文件。

我需要知道这里的最佳做法是什么?

  1. 我是否还需要将文件保存在目录结构中,以便 它适用于 Commons 电子邮件?,或者,
  2. 有什么方法可以使用 流式传输内容本身以添加为附件?

【问题讨论】:

    标签: java jakarta-mail apache-commons-email


    【解决方案1】:

    使用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();
    

    【讨论】:

    • 谢谢,工作正常。虽然我不知道文件描述的用途。我在收到的电子邮件中没有看到任何地方。
    猜你喜欢
    • 2019-12-02
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 2019-06-05
    • 2011-08-04
    • 2011-02-03
    • 1970-01-01
    相关资源
    最近更新 更多