Question

I'm trying to customize the Liferay Web Form portlet to accept file upload, and I've gotten pretty much everything working except attaching the file to the email to be sent.

Getting fields from the form:

public void saveData(ActionRequest actionRequest, ActionResponse actionResponse) {

...

File uploadedFile = null;

...

Map<String,String> fieldsMap = new LinkedHashMap<String,String>();

// Create a FileItemFactory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory(1073741824, new File("/temp"));

// Create a new file upload handler
PortletFileUpload upload = new PortletFileUpload(factory);
upload.setSizeMax(67108864);

// Parse the request
List<FileItem> items = upload.parseRequest(actionRequest); 

int i = 1;

Iterator iter = items.iterator();

// Throw away the hidden field, don't need it
FileItem dud = (FileItem)iter.next();

while (iter.hasNext()) {

    FileItem item = (FileItem)iter.next();

    String fieldLabel = preferences.getValue(
        "fieldLabel" + i, StringPool.BLANK);

    String fieldType = preferences.getValue(
            "fieldType" + i, StringPool.BLANK);

    if (Validator.isNull(fieldLabel)) {
        break;
    }

    if(!fieldType.equals("file")) {
        String fieldValue = item.getString();
        fieldsMap.put(fieldLabel, fieldValue);
    } else {
        String fieldName = item.getName();
        uploadedFile = new File(fieldName);
        item.write(uploadedFile);
    }
        i++;
}
...

     if(sendAsEmail) {
         emailSuccess = sendEmail(fieldsMap, preferences, uploadedFile);
     }
}

And then the construction and sending of the email:

protected boolean sendEmail(Map<String, String> fieldsMap, PortletPreferences preferences, File uploadedFile) {

    MailMessage mailMessage = new MailMessage(fromAddress, toAddress, subject, body, false);

    if(uploadedFile != null) { // i.e., there was 'file' field up above
        mailMessage.addAttachment(uploadedFile);
    }

    MailServiceUtil.sendEmail(mailMessage);


    if(uploadedFile != null) {
       uploadedFile.delete();
    }

}

I get the following console error when attempting to process a file upload and attach the file to the message:

16:09:49,597 ERROR [MailEngine:489] IOException while sending message
16:09:49,598 ERROR [MailEngine:154] java.io.FileNotFoundException: helpdesk_.png (No such file or directory)

Do I have my DiskFileItemFactory configured correctly? What could I be doing wrong?

Thanks.

Was it helpful?

Solution

It appears that MailServiceUtil subsystem spins off a thread to do the actual sending of the email, and does not wait for it to return (hence, thread). So after the MailServiceUtil.sendEmail() call, I was immediately deleting the file to be attached BEFORE it actually got a chance to be sent!

I'm going to look into some type of wrapper for the MailServiceUtil that I can pass the file object into and then once its actually sent do the delete.

OTHER TIPS

Can you try using MailEngine.send method

send( InternetAddress from, InternetAddress[] to, InternetAddress[] cc, InternetAddress[] bcc, InternetAddress[] bulkAddresses, String subject, String body, boolean htmlFormat, InternetAddress[] replyTo, String messageId, String inReplyTo, File[] attachments)

I am not sure if MailEngineUtil.sendEmail will work even if you sort out the problem as you have already discovered its sending to a bus and then a listener would get called(I think its MailMessageListener) which does not read attachement even if you add it in mailMessage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top