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) Copy Email from one IMAP Account to Another

Demonstrates how to copy the email in a mailbox from one account to another.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoImapSrc
    Boolean iSuccess
    Handle hoImapDest
    Boolean iFetchUids
    Variant vMset
    Handle hoMset
    Handle hoFac
    Handle hoMsetAlreadyCopied
    String sStrMsgSet
    Integer iNumUids
    Handle hoSbFlags
    Integer i
    Integer iUid
    String sFlags
    String sMimeStr
    Boolean iSeen
    Boolean iFlagged
    Boolean iAnswered
    Boolean iDraft
    String sTemp1
    Boolean bTemp1

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

    // This example assumes Chilkat Imap to have been previously unlocked.
    // See Unlock Imap for sample code.

    // Connect to our source IMAP server.
    Set ComSsl Of hoImapSrc To True
    Set ComPort Of hoImapSrc To 993
    Get ComConnect Of hoImapSrc "MY-IMAP-DOMAIN" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoImapSrc To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Login to the source IMAP server
    Get ComLogin Of hoImapSrc "MY-IMAP-LOGIN" "MY-IMAP-PASSWORD" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoImapSrc To sTemp1
        Showln sTemp1
        Procedure_Return
    End

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

    // Connect to our destination IMAP server.
    Set ComSsl Of hoImapDest To True
    Set ComPort Of hoImapDest To 993
    Get ComConnect Of hoImapDest "MY-IMAP-DOMAIN2" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoImapDest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Login to the destination IMAP server
    Get ComLogin Of hoImapDest "MY-IMAP-LOGIN2" "MY-IMAP-PASSWORD2" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoImapDest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Select a source IMAP mailbox on the source IMAP server
    Get ComSelectMailbox Of hoImapSrc "Inbox" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoImapSrc To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move True To iFetchUids

    // Get the set of UIDs for all emails on the source server.
    Get ComSearch Of hoImapSrc "ALL" iFetchUids To vMset
    If (IsComObject(vMset)) Begin
        Get Create (RefClass(cComChilkatMessageSet)) To hoMset
        Set pvComObject Of hoMset To vMset
    End
    Get ComLastMethodSuccess Of hoImapSrc To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoImapSrc To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Load the complete set of UIDs that were previously copied.
    // We dont' want to copy any of these to the destination.
    Get Create (RefClass(cComCkFileAccess)) To hoFac
    If (Not(IsComObjectCreated(hoFac))) Begin
        Send CreateComObject of hoFac
    End
    Get Create (RefClass(cComChilkatMessageSet)) To hoMsetAlreadyCopied
    If (Not(IsComObjectCreated(hoMsetAlreadyCopied))) Begin
        Send CreateComObject of hoMsetAlreadyCopied
    End
    Get ComReadEntireTextFile Of hoFac "qa_cache/saAlreadyLoaded.txt" "utf-8" To sStrMsgSet
    Get ComLastMethodSuccess Of hoFac To bTemp1
    If (bTemp1 = True) Begin
        Get ComFromCompactString Of hoMsetAlreadyCopied sStrMsgSet To iSuccess
    End

    Get ComCount Of hoMset To iNumUids
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbFlags
    If (Not(IsComObjectCreated(hoSbFlags))) Begin
        Send CreateComObject of hoSbFlags
    End

    Move 0 To i
    While (i < iNumUids)

        // If this UID was not already copied...
        Get ComGetId Of hoMset i To iUid
        Get ComContainsId Of hoMsetAlreadyCopied iUid To bTemp1
        If (Not bTemp1) Begin

            Showln "copying " iUid "..."

            // Get the flags.
            Get ComFetchFlags Of hoImapSrc iUid True To sFlags
            Get ComLastMethodSuccess Of hoImapSrc To bTemp1
            If (bTemp1 = False) Begin
                Get ComLastErrorText Of hoImapSrc To sTemp1
                Showln sTemp1
                Procedure_Return
            End

            Get ComSetString Of hoSbFlags sFlags To iSuccess

            // Get the MIME of this email from the source.
            Get ComFetchSingleAsMime Of hoImapSrc iUid True To sMimeStr
            Get ComLastMethodSuccess Of hoImapSrc To bTemp1
            If (bTemp1 = False) Begin
                Get ComLastErrorText Of hoImapSrc To sTemp1
                Showln sTemp1
                Procedure_Return
            End

            Get ComContains Of hoSbFlags "\Seen" False To iSeen
            Get ComContains Of hoSbFlags "\Flagged" False To iFlagged
            Get ComContains Of hoSbFlags "\Answered" False To iAnswered
            Get ComContains Of hoSbFlags "\Draft" False To iDraft

            Get ComAppendMimeWithFlags Of hoImapDest "Inbox" sMimeStr iSeen iFlagged iAnswered iDraft To iSuccess
            If (iSuccess <> True) Begin
                Get ComLastErrorText Of hoImapDest To sTemp1
                Showln sTemp1
                Procedure_Return
            End

            // Update msetAlreadyCopied with the uid just copied.
            Send ComInsertId To hoMsetAlreadyCopied iUid

            // Save at every iteration just in case there's a failure..
            Get ComToCompactString Of hoMsetAlreadyCopied To sStrMsgSet
            Get ComWriteEntireTextFile Of hoFac "qa_cache/saAlreadyLoaded.txt" sStrMsgSet "utf-8" False To iSuccess
        End

        Move (i + 1) To i
    Loop

    Send Destroy of hoMset

    // Disconnect from the IMAP servers.
    Get ComDisconnect Of hoImapSrc To iSuccess
    Get ComDisconnect Of hoImapDest To iSuccess


End_Procedure

 

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