Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
Find and save an attachment and list inline images
See more Email Object Examples
Find an attachment by filename and media type, then save its decoded bytes to a path you choose. Enumerate inline resources separately to read their Content-IDs.
Chilkat Lazarus Pascal 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.StringBuilder,
Chilkat.BinData,
Chilkat.Email;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
email: TEmail;
filename: TStringBuilder;
mediaType: TStringBuilder;
found: Boolean;
i: Integer;
data: TBinData;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
email := TEmail.Create;
success := email.LoadEml('with-attachments.eml');
if (success <> True) then
begin
WriteLn(email.LastErrorText);
Exit;
end;
filename := TStringBuilder.Create;
mediaType := TStringBuilder.Create;
found := False;
i := 0;
while (i < email.NumAttachments) do
begin
filename.SetString(email.GetAttachmentFilename(i));
mediaType.SetString(email.GetAttachmentContentType(i));
if ((filename.ContentsEqual('agenda.csv',False) = True) and (mediaType.ContentsEqual('text/csv',False) = True) and (found = False)) then
begin
data := TBinData.Create;
success := email.GetAttachmentBd(i,data);
if (success <> True) then
begin
WriteLn(email.LastErrorText);
Exit;
end;
success := data.WriteFile('received-agenda.csv');
if (success <> True) then
begin
WriteLn('Failed: data.WriteFile.');
Exit;
end;
WriteLn('Saved attachment: ' + data.NumBytes + ' bytes.');
found := True;
end;
i := i + 1;
end;
if (found <> True) then
begin
WriteLn('No matching attachment.');
end;
i := 0;
while (i < email.NumRelatedItems) do
begin
WriteLn('Related: ' + email.GetRelatedContentType(i) + ' Content-ID: ' + email.GetRelatedContentID(i));
i := i + 1;
end;
email.Free;
filename.Free;
mediaType.Free;
data.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.