Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
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 Unicode C++ Downloads
#include <CkImapW.h>
#include <CkMessageSetW.h>
#include <CkEmailW.h>
#include <CkStringBuilderW.h>
#include <CkBinDataW.h>
#include <CkCrypt2W.h>
#include <CkXmlW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkImapW imap;
imap.put_Ssl(true);
imap.put_Port(993);
imap.put_RequireSslCertVerify(true);
imap.put_PeekMode(true);
imap.put_AutoDownloadAttachments(true);
success = imap.Connect(L"imap.example.com");
if (success != true) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
success = imap.Login(L"IMAP_USERNAME",L"IMAP_PASSWORD");
if (success != true) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
success = imap.SelectMailbox(L"INBOX/postacert");
if (success != true) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
CkMessageSetW messages;
success = imap.QueryMbx(L"SUBJECT \"POSTA CERTIFICATA: Invio File\"",true,messages);
if (success != true) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
if (messages.get_Count() == 0) {
wprintf(L"No matching PEC envelope.\n");
imap.Disconnect();
return;
}
CkEmailW envelope;
success = imap.FetchEmail(false,messages.GetId(0),true,envelope);
if (success != true) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
imap.Disconnect();
CkEmailW nested;
CkStringBuilderW name;
int verified = 0;
int i = 0;
while ((i < envelope.get_NumAttachedMessages())) {
success = envelope.GetAttachedEmail(i,nested);
if (success != true) {
wprintf(L"%s\n",envelope.lastErrorText());
return;
}
int j = 0;
while ((j < nested.get_NumAttachments())) {
name.SetString(nested.getAttachmentFilename(j));
if (name.EndsWith(L".xml.p7m",false) == true) {
CkBinDataW data;
success = nested.GetAttachmentBd(j,data);
if (success != true) {
wprintf(L"%s\n",nested.lastErrorText());
return;
}
CkCrypt2W cms;
success = cms.OpaqueVerifyBd(data);
if (success != true) {
wprintf(L"%s\n",cms.lastErrorText());
return;
}
CkXmlW invoice;
success = invoice.LoadBd(data,true);
if (success != true) {
wprintf(L"%s\n",invoice.lastErrorText());
return;
}
wprintf(L"%s XML root: %s\n",name.getAsString(),invoice.tag());
verified = verified + 1;
}
j = j + 1;
}
i = i + 1;
}
wprintf(L"Verified XML invoices: %d\n",verified);
}