File upload / email via Java
Here are some notes on something I had to do today. There are some resources out there on the web which I have used to do it.
The things I needed to do were:
1. write an HTML form which would allow you to add a file to the form which would be sent to the server on Submit
2. write the server-side code to get the uploaded file
3. take the uploaded file and email it somewhere as an attachment.
OK - here's the HTML form (check the source!):
There are various other things you can specify on the form e.g. a max size.
On the server side, here are the basics of the code, with all the extraneous stuff taken out.
Create a MultipartRequest (in com.oreilly.servlet.*), get the file,
then create a multi-part MIME message to send it...
First you have to create a MultipartRequest, a convenience class provided from Jason Hunter's Servlets.com site:
File f = null;
MultipartRequest mprequest = new MultipartRequest(request,"C:\\upload");
Enumeration files = mprequest.getFileNames();
while (files.hasMoreElements()) {
String fileName = (String)files.nextElement();
f = mprequest.getFile(fileName);
}
Then create the mail properties - example follows:
Properties props = new Properties();
props.put("mail.smtp.host", "10.248.2.2");
java.util.Date today = new java.util.Date();
int m = today.getMonth()+1;
int y = today.getYear()-100;
String tDay = "["+today.getDate()+"/"+m+"/0"+y+"] ";
String remSummary = "["+country+"] "+tDay+summary;
We need a session object... not quite sure why at the moment...
Session s = Session.getDefaultInstance(props,null);
Session s2 = Session.getDefaultInstance(props,null);
Then create the messages themselves.
Note that the setRecipients method expects an array of Addresses.
Then you have to create a message with multiple parts for the body and attachment. The way to do it is below... I haven't understood it in great depth but anyway... note the data source handling for the attachment types...
Message msg = new MimeMessage(s);
InternetAddress fromAddr = new InternetAddress(email);
msg.setFrom(fromAddr);
InternetAddress[] toAddr = new InternetAddress[]{new InternetAddress("sendto@my.com")};
msg.setRecipients(Message.RecipientType.TO,toAddr);
msg.setSubject("Request: " + summary);
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(description, "text/plain");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
MimeBodyPart file_part = new MimeBodyPart();
FileDataSource fds = new FileDataSource(f);
DataHandler dh = new DataHandler(fds);
file_part.setFileName(f.getName());
file_part.setDisposition(Part.ATTACHMENT);
file_part.setDescription("Attached file: " + f.getName());
file_part.setDataHandler(dh);
multipart.addBodyPart(file_part);
msg.setContent(multipart);
Transport.send(msg);
MimeMessage message2 = new MimeMessage(s2);
message2.setContent(htmlText, "text/html");
InternetAddress from2 = new InternetAddress("sendto@my.com");
message2.setFrom(from2);
InternetAddress to2 = new InternetAddress(email);
message2.addRecipient(Message.RecipientType.TO, to2);
message2.setSubject("Confirmation of Your Request");
Transport.send(message2);










