Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Extract signed invoice XML from an Italian PEC message

See more Email Object Examples

Search the PEC folder by subject, fetch one envelope by UID, and inspect the attachments of each nested email. Match .xml.p7m by filename and verify the opaque CMS signature before loading the XML. This checks signature integrity. Certificate trust and invoice schema validation are separate application decisions.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.MessageSet,
  Chilkat.Imap,
  Chilkat.Email,
  Chilkat.StringBuilder,
  Chilkat.BinData,
  Chilkat.Xml,
  Chilkat.Crypt2;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  imap: TImap;
  messages: TMessageSet;
  envelope: TEmail;
  nested: TEmail;
  name: TStringBuilder;
  verified: Integer;
  i: Integer;
  j: Integer;
  data: TBinData;
  cms: TCrypt2;
  invoice: TXml;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.

  imap := TImap.Create;
  imap.Ssl := True;
  imap.Port := 993;
  imap.RequireSslCertVerify := True;
  imap.PeekMode := True;
  imap.AutoDownloadAttachments := True;
  success := imap.Connect('imap.example.com');
  if (success <> True) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  success := imap.Login('IMAP_USERNAME','IMAP_PASSWORD');
  if (success <> True) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  success := imap.SelectMailbox('INBOX/postacert');
  if (success <> True) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  messages := TMessageSet.Create;
  success := imap.QueryMbx('SUBJECT "POSTA CERTIFICATA: Invio File"',True,messages);
  if (success <> True) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  if (messages.Count = 0) then
    begin
      WriteLn('No matching PEC envelope.');
      imap.Disconnect();
      Exit;
    end;
  envelope := TEmail.Create;
  success := imap.FetchEmail(False,messages.GetId(0),True,envelope);
  if (success <> True) then
    begin
      WriteLn(imap.LastErrorText);
      Exit;
    end;
  imap.Disconnect();
  nested := TEmail.Create;
  name := TStringBuilder.Create;
  verified := 0;
  i := 0;
  while (i < envelope.NumAttachedMessages) do
    begin
      success := envelope.GetAttachedEmail(i,nested);
      if (success <> True) then
        begin
          WriteLn(envelope.LastErrorText);
          Exit;
        end;
      j := 0;
      while (j < nested.NumAttachments) do
        begin
          name.SetString(nested.GetAttachmentFilename(j));
          if (name.EndsWith('.xml.p7m',False) = True) then
            begin
              data := TBinData.Create;
              success := nested.GetAttachmentBd(j,data);
              if (success <> True) then
                begin
                  WriteLn(nested.LastErrorText);
                  Exit;
                end;
              cms := TCrypt2.Create;
              success := cms.OpaqueVerifyBd(data);
              if (success <> True) then
                begin
                  WriteLn(cms.LastErrorText);
                  Exit;
                end;
              invoice := TXml.Create;
              success := invoice.LoadBd(data,True);
              if (success <> True) then
                begin
                  WriteLn(invoice.LastErrorText);
                  Exit;
                end;
              WriteLn(name.GetAsString() + '  XML root: ' + invoice.Tag);
              verified := verified + 1;
            end;
          j := j + 1;
        end;

      i := i + 1;
    end;

  WriteLn('Verified XML invoices: ' + verified);


  imap.Free;
  messages.Free;
  envelope.Free;
  nested.Free;
  name.Free;
              data.Free;
              cms.Free;
              invoice.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.