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

SQL Server 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
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

 

 

 

(SQL Server) 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

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @imapSrc int
    -- Use "Chilkat_9_5_0.Imap" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Imap', @imapSrc OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

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

    -- Connect to our source IMAP server.
    EXEC sp_OASetProperty @imapSrc, 'Ssl', 1
    EXEC sp_OASetProperty @imapSrc, 'Port', 993
    DECLARE @success int
    EXEC sp_OAMethod @imapSrc, 'Connect', @success OUT, 'MY-IMAP-DOMAIN'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @imapSrc, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imapSrc
        RETURN
      END

    -- Login to the source IMAP server
    EXEC sp_OAMethod @imapSrc, 'Login', @success OUT, 'MY-IMAP-LOGIN', 'MY-IMAP-PASSWORD'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @imapSrc, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imapSrc
        RETURN
      END

    DECLARE @imapDest int
    -- Use "Chilkat_9_5_0.Imap" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Imap', @imapDest OUT

    -- Connect to our destination IMAP server.
    EXEC sp_OASetProperty @imapDest, 'Ssl', 1
    EXEC sp_OASetProperty @imapDest, 'Port', 993
    EXEC sp_OAMethod @imapDest, 'Connect', @success OUT, 'MY-IMAP-DOMAIN2'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @imapDest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imapSrc
        EXEC @hr = sp_OADestroy @imapDest
        RETURN
      END

    -- Login to the destination IMAP server
    EXEC sp_OAMethod @imapDest, 'Login', @success OUT, 'MY-IMAP-LOGIN2', 'MY-IMAP-PASSWORD2'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @imapDest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imapSrc
        EXEC @hr = sp_OADestroy @imapDest
        RETURN
      END

    -- Select a source IMAP mailbox on the source IMAP server
    EXEC sp_OAMethod @imapSrc, 'SelectMailbox', @success OUT, 'Inbox'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @imapSrc, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imapSrc
        EXEC @hr = sp_OADestroy @imapDest
        RETURN
      END

    DECLARE @fetchUids int
    SELECT @fetchUids = 1

    -- Get the set of UIDs for all emails on the source server.
    DECLARE @mset int
    EXEC sp_OAMethod @imapSrc, 'Search', @mset OUT, 'ALL', @fetchUids
    EXEC sp_OAGetProperty @imapSrc, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @imapSrc, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imapSrc
        EXEC @hr = sp_OADestroy @imapDest
        RETURN
      END

    -- Load the complete set of UIDs that were previously copied.
    -- We dont' want to copy any of these to the destination.
    DECLARE @fac int
    -- Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT

    DECLARE @msetAlreadyCopied int
    -- Use "Chilkat_9_5_0.MessageSet" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.MessageSet', @msetAlreadyCopied OUT

    DECLARE @strMsgSet nvarchar(4000)
    EXEC sp_OAMethod @fac, 'ReadEntireTextFile', @strMsgSet OUT, 'qa_cache/saAlreadyLoaded.txt', 'utf-8'
    EXEC sp_OAGetProperty @fac, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAMethod @msetAlreadyCopied, 'FromCompactString', @success OUT, @strMsgSet
      END

    DECLARE @numUids int
    EXEC sp_OAGetProperty @mset, 'Count', @numUids OUT
    DECLARE @sbFlags int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbFlags OUT

    DECLARE @i int
    SELECT @i = 0
    WHILE @i < @numUids
      BEGIN

        -- If this UID was not already copied...
        DECLARE @uid int
        EXEC sp_OAMethod @mset, 'GetId', @uid OUT, @i
        EXEC sp_OAMethod @msetAlreadyCopied, 'ContainsId', @iTmp0 OUT, @uid
        IF Not @iTmp0
          BEGIN



            PRINT 'copying ' + @uid + '...'

            -- Get the flags.
            DECLARE @flags nvarchar(4000)
            EXEC sp_OAMethod @imapSrc, 'FetchFlags', @flags OUT, @uid, 1
            EXEC sp_OAGetProperty @imapSrc, 'LastMethodSuccess', @iTmp0 OUT
            IF @iTmp0 = 0
              BEGIN
                EXEC sp_OAGetProperty @imapSrc, 'LastErrorText', @sTmp0 OUT
                PRINT @sTmp0
                EXEC @hr = sp_OADestroy @imapSrc
                EXEC @hr = sp_OADestroy @imapDest
                EXEC @hr = sp_OADestroy @fac
                EXEC @hr = sp_OADestroy @msetAlreadyCopied
                EXEC @hr = sp_OADestroy @sbFlags
                RETURN
              END
            EXEC sp_OAMethod @sbFlags, 'SetString', @success OUT, @flags

            -- Get the MIME of this email from the source.
            DECLARE @mimeStr nvarchar(4000)
            EXEC sp_OAMethod @imapSrc, 'FetchSingleAsMime', @mimeStr OUT, @uid, 1
            EXEC sp_OAGetProperty @imapSrc, 'LastMethodSuccess', @iTmp0 OUT
            IF @iTmp0 = 0
              BEGIN
                EXEC sp_OAGetProperty @imapSrc, 'LastErrorText', @sTmp0 OUT
                PRINT @sTmp0
                EXEC @hr = sp_OADestroy @imapSrc
                EXEC @hr = sp_OADestroy @imapDest
                EXEC @hr = sp_OADestroy @fac
                EXEC @hr = sp_OADestroy @msetAlreadyCopied
                EXEC @hr = sp_OADestroy @sbFlags
                RETURN
              END

            DECLARE @seen int
            EXEC sp_OAMethod @sbFlags, 'Contains', @seen OUT, '\Seen', 0
            DECLARE @flagged int
            EXEC sp_OAMethod @sbFlags, 'Contains', @flagged OUT, '\Flagged', 0
            DECLARE @answered int
            EXEC sp_OAMethod @sbFlags, 'Contains', @answered OUT, '\Answered', 0
            DECLARE @draft int
            EXEC sp_OAMethod @sbFlags, 'Contains', @draft OUT, '\Draft', 0

            EXEC sp_OAMethod @imapDest, 'AppendMimeWithFlags', @success OUT, 'Inbox', @mimeStr, @seen, @flagged, @answered, @draft
            IF @success <> 1
              BEGIN
                EXEC sp_OAGetProperty @imapDest, 'LastErrorText', @sTmp0 OUT
                PRINT @sTmp0
                EXEC @hr = sp_OADestroy @imapSrc
                EXEC @hr = sp_OADestroy @imapDest
                EXEC @hr = sp_OADestroy @fac
                EXEC @hr = sp_OADestroy @msetAlreadyCopied
                EXEC @hr = sp_OADestroy @sbFlags
                RETURN
              END

            -- Update msetAlreadyCopied with the uid just copied.
            EXEC sp_OAMethod @msetAlreadyCopied, 'InsertId', NULL, @uid

            -- Save at every iteration just in case there's a failure..
            EXEC sp_OAMethod @msetAlreadyCopied, 'ToCompactString', @strMsgSet OUT
            EXEC sp_OAMethod @fac, 'WriteEntireTextFile', @success OUT, 'qa_cache/saAlreadyLoaded.txt', @strMsgSet, 'utf-8', 0
          END

        SELECT @i = @i + 1
      END
    EXEC @hr = sp_OADestroy @mset

    -- Disconnect from the IMAP servers.
    EXEC sp_OAMethod @imapSrc, 'Disconnect', @success OUT
    EXEC sp_OAMethod @imapDest, 'Disconnect', @success OUT

    EXEC @hr = sp_OADestroy @imapSrc
    EXEC @hr = sp_OADestroy @imapDest
    EXEC @hr = sp_OADestroy @fac
    EXEC @hr = sp_OADestroy @msetAlreadyCopied
    EXEC @hr = sp_OADestroy @sbFlags


END
GO

 

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