使用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("[email protected]", "John Doe");
email.setFrom("[email protected]", "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();
感謝。將嘗試並更新。 – user644745
謝謝,工作得很好。儘管我不知道文件描述是什麼。我沒有看到收到的電子郵件中的任何地方。 – Carcamano