I am using a virtual filesystem and I'd like to attach a file from it to an email. However, the MimeBodyPart object only takes Files, which don't work on a default filesystem like jimfs. See my code below, where I get an UnsupportedOperation exception when I try to convert to file.
public Email attach(Path file){
MimeBodyPart attachment = new MimeBodyPart()
attachment.attachFile(file.toFile())
attachments.add(attachment)
return this
}
Since jimfs files aren't really files, you can't use the File APIs.
A simple workaround is to use ByteArrayDataSource, which will copy the data.
A better approach would be to write your own PathDataSource that's similar to FileDataSource but uses Files.newInputStream instead of FileInputStream. Then attach the file using:
MimeBodyPart mbp = new MimeBodyPart();
mbp.setDataHandler(new DataHandler(new PathDataSource(path)));
mbp.setFileName(path.getFileName().toString());
mbp.setDisposition(Part.ATTACHMENT);