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

PowerBuilder 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

 

 

 

(PowerBuilder) 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

integer li_rc
oleobject loo_ImapSrc
integer li_Success
oleobject loo_ImapDest
integer li_FetchUids
oleobject loo_Mset
oleobject loo_Fac
oleobject loo_MsetAlreadyCopied
string ls_StrMsgSet
integer li_NumUids
oleobject loo_SbFlags
integer i
integer li_Uid
string ls_Flags
string ls_MimeStr
integer li_Seen
integer li_Flagged
integer li_Answered
integer li_Draft

loo_ImapSrc = create oleobject
// Use "Chilkat_9_5_0.Imap" for versions of Chilkat < 10.0.0
li_rc = loo_ImapSrc.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_ImapSrc
    MessageBox("Error","Connecting to COM object failed")
    return
end if

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

// Connect to our source IMAP server.
loo_ImapSrc.Ssl = 1
loo_ImapSrc.Port = 993
li_Success = loo_ImapSrc.Connect("MY-IMAP-DOMAIN")
if li_Success <> 1 then
    Write-Debug loo_ImapSrc.LastErrorText
    destroy loo_ImapSrc
    return
end if

// Login to the source IMAP server
li_Success = loo_ImapSrc.Login("MY-IMAP-LOGIN","MY-IMAP-PASSWORD")
if li_Success <> 1 then
    Write-Debug loo_ImapSrc.LastErrorText
    destroy loo_ImapSrc
    return
end if

loo_ImapDest = create oleobject
// Use "Chilkat_9_5_0.Imap" for versions of Chilkat < 10.0.0
li_rc = loo_ImapDest.ConnectToNewObject("Chilkat.Imap")

// Connect to our destination IMAP server.
loo_ImapDest.Ssl = 1
loo_ImapDest.Port = 993
li_Success = loo_ImapDest.Connect("MY-IMAP-DOMAIN2")
if li_Success <> 1 then
    Write-Debug loo_ImapDest.LastErrorText
    destroy loo_ImapSrc
    destroy loo_ImapDest
    return
end if

// Login to the destination IMAP server
li_Success = loo_ImapDest.Login("MY-IMAP-LOGIN2","MY-IMAP-PASSWORD2")
if li_Success <> 1 then
    Write-Debug loo_ImapDest.LastErrorText
    destroy loo_ImapSrc
    destroy loo_ImapDest
    return
end if

// Select a source IMAP mailbox on the source IMAP server
li_Success = loo_ImapSrc.SelectMailbox("Inbox")
if li_Success <> 1 then
    Write-Debug loo_ImapSrc.LastErrorText
    destroy loo_ImapSrc
    destroy loo_ImapDest
    return
end if

li_FetchUids = 1

// Get the set of UIDs for all emails on the source server.
loo_Mset = loo_ImapSrc.Search("ALL",li_FetchUids)
if loo_ImapSrc.LastMethodSuccess <> 1 then
    Write-Debug loo_ImapSrc.LastErrorText
    destroy loo_ImapSrc
    destroy loo_ImapDest
    return
end if

// Load the complete set of UIDs that were previously copied.
// We dont' want to copy any of these to the destination.
loo_Fac = create oleobject
// Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")

loo_MsetAlreadyCopied = create oleobject
// Use "Chilkat_9_5_0.MessageSet" for versions of Chilkat < 10.0.0
li_rc = loo_MsetAlreadyCopied.ConnectToNewObject("Chilkat.MessageSet")

ls_StrMsgSet = loo_Fac.ReadEntireTextFile("qa_cache/saAlreadyLoaded.txt","utf-8")
if loo_Fac.LastMethodSuccess = 1 then
    loo_MsetAlreadyCopied.FromCompactString(ls_StrMsgSet)
end if

li_NumUids = loo_Mset.Count
loo_SbFlags = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_SbFlags.ConnectToNewObject("Chilkat.StringBuilder")

i = 0
do while i < li_NumUids

    // If this UID was not already copied...
    li_Uid = loo_Mset.GetId(i)
    if not loo_MsetAlreadyCopied.ContainsId(li_Uid) then

        Write-Debug "copying " + string(li_Uid) + "..."

        // Get the flags.
        ls_Flags = loo_ImapSrc.FetchFlags(li_Uid,1)
        if loo_ImapSrc.LastMethodSuccess = 0 then
            Write-Debug loo_ImapSrc.LastErrorText
            destroy loo_ImapSrc
            destroy loo_ImapDest
            destroy loo_Fac
            destroy loo_MsetAlreadyCopied
            destroy loo_SbFlags
            return
        end if

        loo_SbFlags.SetString(ls_Flags)

        // Get the MIME of this email from the source.
        ls_MimeStr = loo_ImapSrc.FetchSingleAsMime(li_Uid,1)
        if loo_ImapSrc.LastMethodSuccess = 0 then
            Write-Debug loo_ImapSrc.LastErrorText
            destroy loo_ImapSrc
            destroy loo_ImapDest
            destroy loo_Fac
            destroy loo_MsetAlreadyCopied
            destroy loo_SbFlags
            return
        end if

        li_Seen = loo_SbFlags.Contains("\Seen",0)
        li_Flagged = loo_SbFlags.Contains("\Flagged",0)
        li_Answered = loo_SbFlags.Contains("\Answered",0)
        li_Draft = loo_SbFlags.Contains("\Draft",0)

        li_Success = loo_ImapDest.AppendMimeWithFlags("Inbox",ls_MimeStr,li_Seen,li_Flagged,li_Answered,li_Draft)
        if li_Success <> 1 then
            Write-Debug loo_ImapDest.LastErrorText
            destroy loo_ImapSrc
            destroy loo_ImapDest
            destroy loo_Fac
            destroy loo_MsetAlreadyCopied
            destroy loo_SbFlags
            return
        end if

        // Update msetAlreadyCopied with the uid just copied.
        loo_MsetAlreadyCopied.InsertId(li_Uid)

        // Save at every iteration just in case there's a failure..
        ls_StrMsgSet = loo_MsetAlreadyCopied.ToCompactString()
        loo_Fac.WriteEntireTextFile("qa_cache/saAlreadyLoaded.txt",ls_StrMsgSet,"utf-8",0)
    end if

    i = i + 1
loop
destroy loo_Mset

// Disconnect from the IMAP servers.
li_Success = loo_ImapSrc.Disconnect()
li_Success = loo_ImapDest.Disconnect()


destroy loo_ImapSrc
destroy loo_ImapDest
destroy loo_Fac
destroy loo_MsetAlreadyCopied
destroy loo_SbFlags

 

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