I am using the apache commons fileupload for uploading the file. I want to attach this file to an email. I don't want to write to temp file but I want to keep the data in memory and send it as an attachment. I need direction from here. Thanks in advance
DiskFileItemFactory factory = new DiskFileItemFactory();
PortletFileUpload upload = new PortletFileUpload(factory);
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while(iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if(item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
response.setRenderParameter(name, value);
} else {
String fieldName = item.getFieldName();
String fileName = item.getName();
//String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
InputStream uploadedStream = item.getInputStream();
}
}
UPDATE I have the following method signature for sending an email with attachement and its working fine.
sendWithFileAttachment (String[] recipients,
String subject,
File message,
String from,
String filename) {
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("Pardon Ideas");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(message);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(multipart);
Transport.send(msg);
}
UPDATE 2: I am getting the below error after implementing your code. Can you please help me with this
HIT ME! 15782
Jul 31, 2012 11:17:56 AM test.test.EmailUtility1$InputStreamMimeBodyPart getContentStream
SEVERE: null
Throwable occurred: java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:156)
at java.io.BufferedInputStream.reset(BufferedInputStream.java:425)
at test.test.EmailUtility1$InputStreamMimeBodyPart.getContentStream(EmailUtility1.java:174)
at javax.mail.internet.MimePartDataSource.getInputStream(MimePartDataSource.java:94)
at javax.activation.DataHandler.writeTo(DataHandler.java:302)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:845)
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:361)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:85)
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:881)
at javax.activation.DataHandler.writeTo(DataHandler.java:314)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1683)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:585)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at test.test.EmailUtility1.sendWithFileAttachment(EmailUtility1.java:155)
at test.test.TestEmail.main(TestEmail.java:32)
Exception in thread "main" javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.IOException: Stream closed
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:625)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at test.test.EmailUtility1.sendWithFileAttachment(EmailUtility1.java:155)
at test.test.TestEmail.main(TestEmail.java:32)
Caused by: java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:156)
at java.io.BufferedInputStream.read(BufferedInputStream.java:319)
at java.io.FilterInputStream.read(FilterInputStream.java:101)