I am using Apache Commons Email in my web-application and it works fine.
Now that I need to send a document by attachment, I am facing some problems. I need to get the file from the database (as a BLOB) and add it as an attachment. It seems like Commons Email does not support stream attachment and it only takes a file from a path.
I need to know what is the best practice here?
Using MultiPartEmail#attach(DataSource ds, String name, String description) should work:
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With