我使用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