Sample code for 30+ languages & platforms
Delphi ActiveX 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 Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
imap: TChilkatImap;
messages: TMessageSet;
envelope: TChilkatEmail;
nested: TChilkatEmail;
name: TChilkatStringBuilder;
verified: Integer;
i: Integer;
j: Integer;
data: TChilkatBinData;
cms: TChilkatCrypt2;
invoice: TChilkatXml;

begin
success := 0;

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

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

    i := i + 1;
  end;

Memo1.Lines.Add('Verified XML invoices: ' + IntToStr(verified));