Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Save All Email Attachments to a Directory
See more Email Object Examples
Demonstrates the Chilkat Email.SaveAllAttachments method, which saves all of the email's attachments to a directory. If the directory or any of its components do not exist, Chilkat creates them automatically. This example adds two attachments and saves them.
Background: Each attachment is written using its own filename. Because those filenames come from the sender and may collide, consider the
OverwriteExisting property (which can auto-generate unique names) and RemoveAttachmentPaths (which strips embedded path info) to keep saved files safe and non-clobbering — important when processing untrusted incoming mail.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Email;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
email: TEmail;
begin
success := False;
// Demonstrates the SaveAllAttachments method, which saves all attachments to a directory.
// If the directory (or any of its components) does not exist, Chilkat creates it.
email := TEmail.Create;
email.Subject := 'Save all attachments';
email.AddStringAttachment('a.txt','first attachment');
email.AddStringAttachment('b.txt','second attachment');
// Save every attachment into the specified directory.
success := email.SaveAllAttachments('qa_output/attachments');
if (success = False) then
begin
WriteLn(email.LastErrorText);
Exit;
end;
WriteLn('Saved all attachments.');
// Note: The path "qa_output/..." is a relative local filesystem path,
// relative to the current working directory of the running application.
email.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.