RE: Generated PDF attach to Email

pradip choudhari, modified 5 Years ago. Junior Member Posts: 59 Join Date: 5/10/19 Recent Posts
Hello Guys , I am learning  MailServices of Liferay . In that we get option to adding attachment files also , so I tried to add the pdf with Email . But I am not understanding how to add the File object in mailMessage.addFileAttachment(file)  because I am not saving pdf File object anywere in server I will try to directly create the pdf using iText . I am adding my sample code below .Could you please tell the process for resovle this issue.This is great help to me.
pradip choudhari, modified 5 Years ago. Junior Member Posts: 59 Join Date: 5/10/19 Recent Posts
Hello Guys , 
I took some reference code But in that PDF file is already placed in resource folder , so that is not my requirement , because in my case I generating dynamically all data always , Could you please tell the process for resovle this issue.This is great help to me.   I am attaching below snap where I tried that code and I get attachment with mail But  that pdf is alwyas same which is placed inside resource folder. so How to get always newly generated pdf File ?
thumbnail
Mohammed Yasin, modified 5 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
Hi,You can create a Temp file dynamically write on it  from PDF writer and use that in attachment , something like below  
File file = FileUtil.createTempFile("pdf");
OutputStream os = null;
        try {
            os = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            _log.error(e.getMessage());
        }
PdfWriter writer = null;
        try {
            writer = PdfWriter.getInstance(document, os);
        } catch (com.itextpdf.text.DocumentException e) {
            _log.error(e.getMessage());
        }
pradip choudhari, modified 5 Years ago. Junior Member Posts: 59 Join Date: 5/10/19 Recent Posts
Hello Yasin , 
Thanks for your valuable suggestions . 

I tried this approach , It working fine .. But we any other alternate because we dont want to store the file in server . In this code we are creating tempFile i.e. always storing in server .

Thnak & Regards , 
Pradip 
thumbnail
Olaf Kock, modified 5 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
pradip choudhari:

I tried this approach , It working fine .. But we any other alternate because we dont want to store the file in server . In this code we are creating tempFile i.e. always storing in server .
Looking at MailMessage, it seems to be limited so that you can only add attachments from a file - To generate in memory you'd go with e.g. BufferedOutputStream, but MailMessage's interface always assumes that there's a file to attach.
You could go with the standard (javax) mail API instead of Liferay's, but you'd be missing the mail to be asynchronously sent.