Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Rei Mat
Add attachment to Email
January 27, 2011 5:59 AM
Answer

Rei Mat

Rank: Junior Member

Posts: 44

Join Date: September 29, 2010

Recent Posts

Hello,

I would like to send an Email in one of my service.

I use this code and it is working fine:
1InternetAddress from = new InternetAddress(fromAddress, fromName);
2InternetAddress to = new InternetAddress(toAddress, toName);
3MailMessage message = new MailMessage(from, to, subject, body, true);
4MailServiceUtil.sendEmail(message);


But I would like to add an attachment to this email:

I use the code:
1MailMessage message = new MailMessage(from, to, subject, body, true);
2File attachment = new File("com/ordner/content/file.pdf");
3message.addAttachment(attachment);
4MailServiceUtil.sendEmail(message);


But I don't know what path should I use... and where I should put this file on the server.

Thanks and regards,
Mat
Rei Mat
RE: Add attachment to Email
February 10, 2011 8:26 AM
Answer

Rei Mat

Rank: Junior Member

Posts: 44

Join Date: September 29, 2010

Recent Posts

Hello,

Here my solution:

MailMessage message = new MailMessage(from, to, subject, body, true);

InputStream is = getClass().getResourceAsStream("/content/agb.pdf");

 1File tmp2 = null;
 2OutputStream output = null;
 3try {
 4    tmp2 = File.createTempFile("agb", ".pdf");
 5    output = new FileOutputStream(tmp2);
 6    IOUtils.copy(is, output);
 7} catch (IOException e) {
 8         e.printStackTrace();
 9} finally {
10    try {
11    is.close();
12    output.flush();
13    output.close();
14    } catch (Exception e) {
15    e.printStackTrace();
16    }
17}
18message.addAttachment(tmp2);


The attachment has a name with a long ID... We could find a olution in order to rename the file ! But it is not really important!

Regards,
Matthieu
Joe Chang
RE: Add attachment to Email
December 17, 2011 9:02 PM
Answer

Joe Chang

Rank: New Member

Posts: 11

Join Date: August 14, 2011

Recent Posts

Hi,

Could I set the character encode of the attachment file name? When the user upload the chinese file name and send it with the email api. The file name is ??.pdf.

Thanks!
bo li
RE: Add attachment to Email
May 10, 2012 10:02 AM
Answer

bo li

Rank: Junior Member

Posts: 34

Join Date: November 14, 2011

Recent Posts

Hi Matthieu

Have u found a solution for renaming the attachment file?
Jitendra Rajput
RE: Add attachment to Email
May 10, 2012 10:47 PM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 647

Join Date: January 7, 2011

Recent Posts

You can rename the uploaded file . try with below snippet

 1
 2       File sourceFile =  uploadRequest.getFile("file");
 3        File destination = null;
 4        String path = sourceFile.getPath();
 5        path = path.substring(0, path.lastIndexOf(StringPool.BACK_SLASH) + 1);
 6        String fileNm = uploadRequest.getFileName("file");
 7        if (Validator.isNotNull(fileNm))
 8        {
 9            path = path.concat(fileNm);
10            destination = new File(path);
11            FileUtil.copyFile(sourceFile, destination);
12            FileUtil.delete(sourceFile);
13        }
bo li
RE: Add attachment to Email
May 11, 2012 8:24 AM
Answer

bo li

Rank: Junior Member

Posts: 34

Join Date: November 14, 2011

Recent Posts

Thanks, Jitendra, it works pretty well.
Amos Fong
RE: Add attachment to Email
May 15, 2012 12:00 AM
Answer

Amos Fong

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1844

Join Date: October 7, 2008

Recent Posts

Here is my solution if you don't want to make to do any file manipulation:

1        MyFile file = new MyFile(filePath);
2
3        file.setRealName(fileName);
4
5        message.addAttachment(file);

 1
 2public class MyFile extends java.io.File {
 3
 4    public MyFile(String pathname) {
 5        super(pathname);
 6    }
 7
 8    @Override
 9    public String getName() {
10        return _realFileName;
11    }
12
13    public void setRealName(String realFileName) {
14        _realFileName = realFileName;
15    }
16
17    private String _realFileName;
18
19}