How to set attachment name with JavaMail

A

Adam Lipscombe

Folks,


I am using java mail to send out file attachments. A code snippet is:

-----------
String attachmentPath = "C:\temp\MyFile.txt";
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachmentPath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachmentPath);
-----------



But when the file arrives in the email and the user tries to save it, the file name that is
presented is a munged version of the full path. e.g. "c--temp-MyFile.txt"


I would like the email client to present simply "MyFile.txt".
Is there anyway I can achieve this?



TIA -Adam
 
V

voorth

But when the file arrives in the email and the user tries to save it, the file name that is
presented is a munged version of the full path. e.g. "c--temp-MyFile.txt"

I would like the email client to present simply "MyFile.txt".
Is there anyway I can achieve this?

TIA -Adam

In this case, you need the specific behaviour for FileDataSource:

-----------
String attachmentPath = "C:\temp\MyFile.txt";
messageBodyPart = new MimeBodyPart();
FileDataSource source = new FileDataSource(attachmentPath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getFile().getName());
-----------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,627
Members
45,328
Latest member
66Teonna9

Latest Threads

Top