I need to get an excel spreadsheet over HTTP and then send it as an attachment to an email on my Java Spring server.
The problem I am finding is that MultiPartEmail.attach() only takes a java.net.URL instance, and I cannot figure out how to ensure that the request has a certain Cookie in its headers for authentication.
url = new URL(urlString);
email.attach(url, "test.xls", "File");
email.send();
I have attempted to manually request and create a Workbook, but then I am stumped on getting the Workbook itself attached to the MultiPartEmail.
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(queryString);
method.setRequestHeader("Cookie", cookie);
client.executeMethod(method);
InputStream stream = method.getResponseBodyAsStream();
Workbook workbook = Workbook.getWorkbook(stream);
email.attach(workbook, "report.xls", "forecasting report");
I need some way to work around these limitations.
Thanks in advance for your time.
Since you are using Spring, you can use its built-in email support to send emails. So it does not matter how you are retrieving the file from disk or somewhere else. You can send the email with attachments using MimeMessageHelper and specify the username and password for the account at the mail host for authentication like this:
and configure a JavaMailSenderImpl bean in your bean configuration file. This is to send email via Gmail.
beans.xml
and this is just for testing:
Spring Documentation