Fóruns

Início » Liferay Portal » English » 3. Development

Visualização combinada Visão plana Exibição em árvore
Tópicos [ Anterior | Próximo ]
toggle
Rei Mat
Add attachment to Email
27 de Janeiro de 2011 05:59
Resposta

Rei Mat

Ranking: Junior Member

Mensagens: 44

Data de entrada: 29 de Setembro de 2010

Mensagens recentes

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
10 de Fevereiro de 2011 08:26
Resposta

Rei Mat

Ranking: Junior Member

Mensagens: 44

Data de entrada: 29 de Setembro de 2010

Mensagens recentes

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
17 de Dezembro de 2011 21:02
Resposta

Joe Chang

Ranking: New Member

Mensagens: 11

Data de entrada: 14 de Agosto de 2011

Mensagens recentes

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
10 de Maio de 2012 10:02
Resposta

bo li

Ranking: Junior Member

Mensagens: 34

Data de entrada: 14 de Novembro de 2011

Mensagens recentes

Hi Matthieu

Have u found a solution for renaming the attachment file?
Jitendra Rajput
RE: Add attachment to Email
10 de Maio de 2012 22:47
Resposta

Jitendra Rajput

Ranking: Liferay Master

Mensagens: 858

Data de entrada: 7 de Janeiro de 2011

Mensagens recentes

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
11 de Maio de 2012 08:24
Resposta

bo li

Ranking: Junior Member

Mensagens: 34

Data de entrada: 14 de Novembro de 2011

Mensagens recentes

Thanks, Jitendra, it works pretty well.
Amos Fong
RE: Add attachment to Email
15 de Maio de 2012 00:00
Resposta

Amos Fong

LIFERAY STAFF

Ranking: Liferay Legend

Mensagens: 1896

Data de entrada: 7 de Outubro de 2008

Mensagens recentes

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}