Reputation: 6624
I'm trying to create a fil as follows from a URL (my DropBox account), then add it as an attachment to a MimeMessageHelper, but I get a FileNotFoundException
. What could I be doing wrong?
String[] attachments = {"https://dl.dropboxusercontent.com/s/XXXX/my%20Letter.docx"};
for (String attachment : attachments) {
FileSystemResource file = new FileSystemResource("url:" + attachment);
message.addAttachment(file.getFilename(), file);
}
Error:
Caused by: org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.FileNotFoundException: url:https:\dl.dropboxusercontent.com\s\XXXX\my%20Letter.docx (The filename, directory name, or volume label syntax is incorrect); message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.FileNotFoundException: url:https:\dl.dropboxusercontent.com\s\XXXX\my%20Letter.docx (The filename, directory name, or volume label syntax is incorrect)
UPDATE:
How can I go about creating a FileSystemResource
from a Http URL?
Upvotes: 0
Views: 1166
Reputation: 320
You can use UrlResource
instead of File system resource or download the file using some http client and then attach it to email.
For example:
URL website = new URL("http://someurl");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("some temporary name");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
And then attach it using FileSystemResource
You can use http client for this, for example: okhttp
Upvotes: 1