Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Save a MIME Body (Attachment) to a File
See more MIME Examples
Demonstrates the Chilkat Mime.SaveBody method, which writes the decoded body bytes of the current part to a file. The only argument is the file path. Base64 and quoted-printable are decoded; MIME headers and multipart framing are not written.
Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background: This is the direct way to extract a part's content to disk — saving an attachment or the payload of a part as its real, decoded bytes. Chilkat reverses the transfer encoding so the file contains the actual content, not its Base64 form. To reach a specific attachment in a multipart message, navigate to that part (for example with
PartAt) before calling this.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.Mime;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
mime: TMime;
begin
success := False;
mime := TMime.Create;
success := mime.LoadMimeFile('qa_data/message.eml');
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
// Write the decoded body bytes of the current part to a file. Base64 and quoted-printable are
// decoded; MIME headers and multipart framing are not written. This is how you save an
// attachment or the raw content of a part.
success := mime.SaveBody('qa_output/body_content.dat');
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
WriteLn('Body saved.');
mime.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.