Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
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 DLL Downloads
var
success: Boolean;
imap: HCkImap;
messages: HCkMessageSet;
envelope: HCkEmail;
nested: HCkEmail;
name: HCkStringBuilder;
verified: Integer;
i: Integer;
j: Integer;
data: HCkBinData;
cms: HCkCrypt2;
invoice: HCkXml;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
imap := CkImap_Create();
CkImap_putSsl(imap,True);
CkImap_putPort(imap,993);
CkImap_putRequireSslCertVerify(imap,True);
CkImap_putPeekMode(imap,True);
CkImap_putAutoDownloadAttachments(imap,True);
success := CkImap_Connect(imap,'imap.example.com');
if (success <> True) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
success := CkImap_Login(imap,'IMAP_USERNAME','IMAP_PASSWORD');
if (success <> True) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
success := CkImap_SelectMailbox(imap,'INBOX/postacert');
if (success <> True) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
messages := CkMessageSet_Create();
success := CkImap_QueryMbx(imap,'SUBJECT "POSTA CERTIFICATA: Invio File"',True,messages);
if (success <> True) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
if (CkMessageSet_getCount(messages) = 0) then
begin
Memo1.Lines.Add('No matching PEC envelope.');
CkImap_Disconnect(imap);
Exit;
end;
envelope := CkEmail_Create();
success := CkImap_FetchEmail(imap,False,CkMessageSet_GetId(messages,0),True,envelope);
if (success <> True) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
CkImap_Disconnect(imap);
nested := CkEmail_Create();
name := CkStringBuilder_Create();
verified := 0;
i := 0;
while (i < CkEmail_getNumAttachedMessages(envelope)) do
begin
success := CkEmail_GetAttachedEmail(envelope,i,nested);
if (success <> True) then
begin
Memo1.Lines.Add(CkEmail__lastErrorText(envelope));
Exit;
end;
j := 0;
while (j < CkEmail_getNumAttachments(nested)) do
begin
CkStringBuilder_SetString(name,CkEmail__getAttachmentFilename(nested,j));
if (CkStringBuilder_EndsWith(name,'.xml.p7m',False) = True) then
begin
data := CkBinData_Create();
success := CkEmail_GetAttachmentBd(nested,j,data);
if (success <> True) then
begin
Memo1.Lines.Add(CkEmail__lastErrorText(nested));
Exit;
end;
cms := CkCrypt2_Create();
success := CkCrypt2_OpaqueVerifyBd(cms,data);
if (success <> True) then
begin
Memo1.Lines.Add(CkCrypt2__lastErrorText(cms));
Exit;
end;
invoice := CkXml_Create();
success := CkXml_LoadBd(invoice,data,True);
if (success <> True) then
begin
Memo1.Lines.Add(CkXml__lastErrorText(invoice));
Exit;
end;
Memo1.Lines.Add(CkStringBuilder__getAsString(name) + ' XML root: ' + CkXml__tag(invoice));
verified := verified + 1;
end;
j := j + 1;
end;
i := i + 1;
end;
Memo1.Lines.Add('Verified XML invoices: ' + IntToStr(verified));
CkImap_Dispose(imap);
CkMessageSet_Dispose(messages);
CkEmail_Dispose(envelope);
CkEmail_Dispose(nested);
CkStringBuilder_Dispose(name);
CkBinData_Dispose(data);
CkCrypt2_Dispose(cms);
CkXml_Dispose(invoice);