Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Attach a File to a Multipart MIME Entity
See more MIME Examples
Demonstrates the Chilkat Mime.AppendPartFromFile method, which reads a local file, creates an attachment part, and appends it. The only argument is the file path; the content headers are chosen from the filename extension.
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 one-call way to add an attachment: Chilkat reads the file, infers its
Content-Type from the extension, base64-encodes binary content, and appends it as a child part — typically inside a multipart/mixed. It is the building block behind adding attachments to an email, saving you from constructing the part and setting its headers by hand.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;
bodyPart: TMime;
begin
success := False;
// Demonstrates the Mime.AppendPartFromFile method, which reads a local file, creates an attachment
// part, and appends it. The only argument is the file path; the content headers are chosen from
// the filename extension.
mime := TMime.Create;
success := mime.NewMultipartMixed();
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
// A message body part.
bodyPart := TMime.Create;
success := bodyPart.SetBodyFromPlainText('Please see the attached files.');
if (success = False) then
begin
WriteLn(bodyPart.LastErrorText);
Exit;
end;
success := mime.AppendPart(bodyPart);
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
// Attach files directly.
success := mime.AppendPartFromFile('qa_data/report.pdf');
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
success := mime.AppendPartFromFile('qa_data/photo.jpg');
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
WriteLn('Number of parts: ' + mime.NumParts);
mime.Free;
bodyPart.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.