我使用Indy 10和TIdAttachment通過Delphi程序生成帶有附件的電子郵件。文件位置/名稱作爲//server/files/attachments/MyAttachment.pdf存儲在數據庫表中。我的文件附加到電子郵件如下:Delphi - TIdAttachment,從附件的文件名中刪除路徑
// Add each attachment in Attachments
for Attachment in Attachments do begin
// Make sure file name exists before trying to add
if FileExists(Attachment) then
TIdAttachmentFile.Create(MessageParts, Attachment);
end;
當我發送附件文件被命名爲
'__server_files_attachments_MyAttachment.pdf'
的電子郵件。
有沒有辦法刪除文件路徑,使附件在收件人收到郵件時顯示爲'MyAttachment.pdf'?
我嘗試使用ExtractFileName()但沒有運氣。我不認爲它工作的路徑&文件名是作爲一個字符串從數據庫傳來的。
編輯
我也試圖解壓文件名本身如下:
function GetFileName(FullPath: string): string;
var
StrFound: TStringList;
begin
StrFound := TStringList.Create();
ExtractStrings(['/'], [' '], PChar(FullPath), StrFound);
result := StrFound[StrFound.Count - 1];
end;
這將返回「MyAttachment.pdf」但這會導致德爾福尋找的文件夾在其中的程序正在爲不在'// server/files/attachments'中的文件運行。看來,除非我可以在調用TIdAttachmentFile.Create()之後重命名附件,否則我無法更改文件名。
編輯 - 解決方案:
顯示使用雷米的意見的解決方案(和使用GetFileName()
從上面):
// Add each attachment in Attachments
for Attachment in Attachments do begin
// Make sure file name exists before trying to add
if FileExists(Attachment) then begin
with TIdAttachmentFile.Create(MessageParts, Attachment) do begin
Filename := GetFileName(Attachment);
end;
end;
end;
究竟你傳遞給'ExtractFileName'的是什麼? – 2012-03-12 18:11:09
我叫ExtractFileName(附件); 'attachment'是'//server/files/attachments/MyAttachment.pdf' – BrianKE 2012-03-12 18:25:48
如果您將解決方案發布在Remy接受的答案的底部,那會更好。問題只是針對...問題。隨意編輯別人的帖子,SO是一個分享知識的地方;) – bluish 2013-01-23 16:01:18