Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) IMAP Read PEC Email of Aruba and Extract the Invoice XML Files

IMAP Read PEC Email of Aruba and Extract the Invoice XML Files.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoImap
    Boolean iSuccess
    Boolean iFetchUids
    Variant vMessageSet
    Handle hoMessageSet
    Variant vEmail
    Handle hoEmail
    Variant vAttachedEmail
    Handle hoAttachedEmail
    Variant vSbMime
    Handle hoSbMime
    Handle hoMime
    Variant vBdP7m
    Handle hoBdP7m
    Variant vMimeP7m
    Handle hoMimeP7m
    Handle hoCrypt
    Handle hoXml
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End

    // Connect using TLS.
    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993
    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Authenticate
    Get ComLogin Of hoImap "email_account_login" "email_account_password" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Select an IMAP mailbox
    Get ComSelectMailbox Of hoImap "Inbox/postacert" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Search for messages having "POSTA CERTIFICATA: Invio File" in the subject.
    Move True To iFetchUids
    Get ComSearch Of hoImap 'SUBJECT "POSTA CERTIFICATA: Invio File"' iFetchUids To vMessageSet
    If (IsComObject(vMessageSet)) Begin
        Get Create (RefClass(cComChilkatMessageSet)) To hoMessageSet
        Set pvComObject Of hoMessageSet To vMessageSet
    End
    Get ComLastMethodSuccess Of hoImap To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComCount Of hoMessageSet To iTemp1
    If (iTemp1 < 1) Begin
        Showln "No messages found."
        Send Destroy of hoMessageSet
        Procedure_Return
    End

    // For our example, we'll process only the 1st email in the messageSet.
    // Download the MIME source of the email into a StringBuilder object.
    Get ComGetId Of hoMessageSet 0 To iTemp1
    Get ComHasUids Of hoMessageSet To bTemp1
    Get ComFetchSingle Of hoImap iTemp1 bTemp1 To vEmail
    If (IsComObject(vEmail)) Begin
        Get Create (RefClass(cComChilkatEmail)) To hoEmail
        Set pvComObject Of hoEmail To vEmail
    End
    Get ComLastMethodSuccess Of hoImap To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Send Destroy of hoMessageSet
        Procedure_Return
    End

    Send Destroy of hoMessageSet

    // The email should contain an attached email.
    // The Invoice XML is contained within the attached email.
    Get ComGetAttachedMessage Of hoEmail 0 To vAttachedEmail
    If (IsComObject(vAttachedEmail)) Begin
        Get Create (RefClass(cComChilkatEmail)) To hoAttachedEmail
        Set pvComObject Of hoAttachedEmail To vAttachedEmail
    End
    Get ComLastMethodSuccess Of hoEmail To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Send Destroy of hoEmail
        Procedure_Return
    End

    Send Destroy of hoEmail

    // Get the MIME of the attached email..
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbMime
    If (Not(IsComObjectCreated(hoSbMime))) Begin
        Send CreateComObject of hoSbMime
    End
    Get pvComObject of hoSbMime to vSbMime
    Get ComGetMimeSb Of hoAttachedEmail vSbMime To iSuccess
    Send Destroy of hoAttachedEmail

    // Load it into a Chilkat MIME object.
    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End
    Get pvComObject of hoSbMime to vSbMime
    Get ComLoadMimeSb Of hoMime vSbMime To iSuccess

    // Examine the structure of the MIME.
    Get ComGetStructure Of hoMime "text" To sTemp1
    Showln sTemp1

    // The MIME has this structure:
    // multipart/mixed
    //     application/octet-stream
    //     application/octet-stream     <-- This is where the XML is contained.   It is within an opaque signature.
    //     text/plain

    // The 2nd application/octet-stream MIME header looks like this:

    // ------=_Part_329673_-1348225228.1579889273592
    // Content-Type: application/octet-stream; name="SM99999_99aaa.xml.p7m"
    // Content-Transfer-Encoding: base64
    // Content-Disposition: attachment; filename="SM99999_99aaa.xml.p7m"

    // Let's get the binary content of the .p7m
    Get Create (RefClass(cComChilkatBinData)) To hoBdP7m
    If (Not(IsComObjectCreated(hoBdP7m))) Begin
        Send CreateComObject of hoBdP7m
    End
    // Get the 2nd sub-part, at index 1 because index 0 is the 1st sub-part.
    Get ComGetPart Of hoMime 1 To vMimeP7m
    If (IsComObject(vMimeP7m)) Begin
        Get Create (RefClass(cComChilkatMime)) To hoMimeP7m
        Set pvComObject Of hoMimeP7m To vMimeP7m
    End
    Get ComLastMethodSuccess Of hoMime To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Failed to get 2nd sub-part. Perhaps the MIME does not have a 2nd sub-part?"
        Procedure_Return
    End

    Get pvComObject of hoBdP7m to vBdP7m
    Get ComGetBodyBd Of hoMimeP7m vBdP7m To iSuccess
    Send Destroy of hoMimeP7m

    // Verify the signature, which also extracts the content contained within the opaque signature.
    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End
    Get pvComObject of hoBdP7m to vBdP7m
    Get ComOpaqueVerifyBd Of hoCrypt vBdP7m To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCrypt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // The bdP7m now contains the Invoice XML.
    // Load it into an XML object.
    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Get pvComObject of hoBdP7m to vBdP7m
    Get ComLoadBd Of hoXml vBdP7m True To iSuccess

    Get ComGetXml Of hoXml To sTemp1
    Showln sTemp1

    // We have XML such as the following:
    // 
    // <?xml version="1.0" encoding="windows-1252"?>
    // <?xml-stylesheet type="text/xsl" href="fatturapa_v1.2.xsl"?>
    // <p:FatturaElettronica versione="FPR12" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
    //     xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" 
    //     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    //     <FatturaElettronicaHeader>
    // 	...
    //     </FatturaElettronicaHeader>
    //     <FatturaElettronicaBody>
    // 	...
    //     </FatturaElettronicaBody>
    // </p:FatturaElettronica>

    // Use this online tool to generate parsing code from sample XML: 
    // Generate Parsing Code from XML

    Showln "success."


End_Procedure

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.