Sample code for 30+ languages & platforms
DataFlex

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

See more Email Object Examples

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

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

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

    Move False To iSuccess

    // 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 Create (RefClass(cComChilkatMessageSet)) To hoMessageSet
    If (Not(IsComObjectCreated(hoMessageSet))) Begin
        Send CreateComObject of hoMessageSet
    End
    Get pvComObject of hoMessageSet to vMessageSet
    Get ComQueryMbx Of hoImap 'SUBJECT "POSTA CERTIFICATA: Invio File"' iFetchUids vMessageSet To iSuccess
    If (iSuccess = 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."
        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 Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Get ComGetId Of hoMessageSet 0 To iTemp1
    Get ComHasUids Of hoMessageSet To bTemp1
    Get pvComObject of hoEmail to vEmail
    Get ComFetchEmail Of hoImap False iTemp1 bTemp1 vEmail To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // The email should contain an attached email.
    // The Invoice XML is contained within the attached email.
    Get Create (RefClass(cComChilkatEmail)) To hoAttachedEmail
    If (Not(IsComObjectCreated(hoAttachedEmail))) Begin
        Send CreateComObject of hoAttachedEmail
    End
    Get pvComObject of hoAttachedEmail to vAttachedEmail
    Get ComGetAttachedEmail Of hoEmail 0 vAttachedEmail To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // 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

    // 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 Create (RefClass(cComChilkatMime)) To hoMimeP7m
    If (Not(IsComObjectCreated(hoMimeP7m))) Begin
        Send CreateComObject of hoMimeP7m
    End
    Get pvComObject of hoMimeP7m to vMimeP7m
    Get ComPartAt Of hoMime 1 vMimeP7m To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    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

    // 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