DataSource将File的绝对路径名称视为附件文件名

时间:2015-12-23 10:08:37

标签: java itext javamail

假设我在位置 D:\ project \ pdf \ 中生成 pdf 文件 testing.pdf

当我使用Java中的 javax.mail API将要附加到邮件中的相同文件发送到收件人邮件地址时,附件的名称包括绝对路径作为名称:

String file = "D:\project\pdf\testing.pdf";
String to = "<DESTINATION EMAIL ADDRESS>";//destination email address
String from = "<YOUR EMAIL ADDRESS>";//source email address
String password = "<YOUR PASSWORD>"; //Your gmail password
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String host = "smtp.gmail.com"; //hostname of the machine that has smtp server

Properties props = System.getProperties();
. 
.
.   // Defining Property, Session,Message Objects and the that 
    // is required to set up and create a message to send as necessary 
.
.
FileDataSource fdatasource = new FileDataSource(file);  //path of the file to be attached 
.
.
 Transport.send(message);

我知道,由于我提供 FileDataSouce 以及文件的绝对路径(位置),因此附加的文件名称被认为是相同的。< / p>

D:\ project \ pdf \ testing.pdf 是下载接收方收到的附件邮件后的文件(只是为了指出问题, testing.pdf 是我想要的文件名)

我的实际问题是我不希望附加文件包含原始文件的绝对路径作为收件人方。

有没有办法使附件的名称只包含实际的文件名(testing.pdf)而不包括绝对路径(D:\ project \ pdf \ testing.pdf)?

1 个答案:

答案 0 :(得分:0)

在正文部分设置数据处理程序后调用javax.mail.internet.MimeBodyPart.setFileName。如果你想要做的就是附加一个文件,你可以使用attachFile方法之一。