2015-12-23 40 views
0

說我產生PDF文件testing.pdf在位置d:\項目\ PDF \的DataSource考慮文件的絕對路徑名作爲附件文件名

當我使用相同的文件,以在郵件附以發送到使用javax.mail API在Java中,附加的文件的名稱包括作爲名稱的絕對路徑收件人郵箱地址:

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絕對路徑(位置),附加的文件的名稱被認爲是相同的事實。

d:\項目\ PDF \ testing.pdf是下載附件那些在收件方郵件收到後的文件(只是指出問題,testing.pdf是所有我想要的文件名是)

我實際的問題是我不希望附加文件包含原始文件作爲收件人側的文件名絕對路徑。

有什麼辦法讓附件名只包含實際的文件名(testing.pdf)而不是絕對路徑(D:\ project \ pdf \ testing.pdf)?

+0

爲什麼不在內存中創建文件(使用'ByteArrayOutputStream'而不是'FileOutputStream'),會不會更優雅?郵件發送完畢後,您將如何處理文件系統上的所有PDF文件?當然有更好的方法來存檔這些PDF文件(假設你需要將它們存檔)? –

+0

@Bruno Lowagie,謝謝你,這是一個有用的建議,是的,檔案是維護的相關信息。讓我試試「ByteArrayOutputStream」..再次感謝。 –

回答