Fórum
Add attachment to Email
Rei Mat, modificado 13 Anos atrás.
Add attachment to Email
Junior Member Postagens: 44 Data de Entrada: 29/09/10 Postagens Recentes
Hello,
I would like to send an Email in one of my service.
I use this code and it is working fine:
But I would like to add an attachment to this email:
I use the code:
But I don't know what path should I use... and where I should put this file on the server.
Thanks and regards,
Mat
I would like to send an Email in one of my service.
I use this code and it is working fine:
InternetAddress from = new InternetAddress(fromAddress, fromName);
InternetAddress to = new InternetAddress(toAddress, toName);
MailMessage message = new MailMessage(from, to, subject, body, true);
MailServiceUtil.sendEmail(message);
But I would like to add an attachment to this email:
I use the code:
MailMessage message = new MailMessage(from, to, subject, body, true);
File attachment = new File("com/ordner/content/file.pdf");
message.addAttachment(attachment);
MailServiceUtil.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, modificado 13 Anos atrás.
RE: Add attachment to Email (Resposta)
Junior Member Postagens: 44 Data de Entrada: 29/09/10 Postagens Recentes
Hello,
Here my solution:
MailMessage message = new MailMessage(from, to, subject, body, true);
InputStream is = getClass().getResourceAsStream("/content/agb.pdf");
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
Here my solution:
MailMessage message = new MailMessage(from, to, subject, body, true);
InputStream is = getClass().getResourceAsStream("/content/agb.pdf");
File tmp2 = null;
OutputStream output = null;
try {
tmp2 = File.createTempFile("agb", ".pdf");
output = new FileOutputStream(tmp2);
IOUtils.copy(is, output);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
output.flush();
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
message.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, modificado 12 Anos atrás.
RE: Add attachment to Email
New Member Postagens: 11 Data de Entrada: 14/08/11 Postagens 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!
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, modificado 11 Anos atrás.
RE: Add attachment to Email
Junior Member Postagens: 38 Data de Entrada: 14/11/11 Postagens Recentes
Hi Matthieu
Have u found a solution for renaming the attachment file?
Have u found a solution for renaming the attachment file?
Jitendra Rajput, modificado 11 Anos atrás.
RE: Add attachment to Email
Liferay Master Postagens: 875 Data de Entrada: 07/01/11 Postagens Recentes
You can rename the uploaded file . try with below snippet
File sourceFile = uploadRequest.getFile("file");
File destination = null;
String path = sourceFile.getPath();
path = path.substring(0, path.lastIndexOf(StringPool.BACK_SLASH) + 1);
String fileNm = uploadRequest.getFileName("file");
if (Validator.isNotNull(fileNm))
{
path = path.concat(fileNm);
destination = new File(path);
FileUtil.copyFile(sourceFile, destination);
FileUtil.delete(sourceFile);
}
bo li, modificado 11 Anos atrás.
RE: Add attachment to Email
Junior Member Postagens: 38 Data de Entrada: 14/11/11 Postagens Recentes
Thanks, Jitendra, it works pretty well.
Amos Fong, modificado 11 Anos atrás.
RE: Add attachment to Email
Liferay Legend Postagens: 2047 Data de Entrada: 07/10/08 Postagens Recentes
Here is my solution if you don't want to make to do any file manipulation:
MyFile file = new MyFile(filePath);
file.setRealName(fileName);
message.addAttachment(file);
public class MyFile extends java.io.File {
public MyFile(String pathname) {
super(pathname);
}
@Override
public String getName() {
return _realFileName;
}
public void setRealName(String realFileName) {
_realFileName = realFileName;
}
private String _realFileName;
}