SQL Server Stored Procedure Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

SQL Server
Stored Procedure Examples

Quick Start
Encryption
File Access
IMAP
POP3
SMTP
Email Object
DKIM / DomainKey
FTP
HTML Conversion
HTTP
MHT
MIME
NTLM
RSA
Diffie-Hellman
DSA
Socket
Spider
SSH Key
SSH
SSH Tunnel
SFTP
String
Tar
Upload
XML
XMP
Zip

Amazon S3
Bz2
CSV
FileAccess
Byte Array
RSS
Atom
Self-Extractor

POP3: Download Most Recent Email (method 2)

How to ready the N most recent email from a POP3 server.

The POP3 protocol does not provide the ability to request the most recent email, nor does it provide the ability to download email based on read/unread status or any other criteria. The design principle behind POP3 is that it is a temporary holding store for incoming email and email clients or other applications will transfer email from the POP3 server to a local persistent store where it will be managed. Therefore, POP3 does not provide sophisticated functionality (as opposed to IMAP which has the opposite design philosophy: that email is maintained and organized on the server).

This example shows one possible way to retrieve the N most recent emails from a POP3 server. It downloads the complete list of UIDLs and then downloads the last N into a bundle object. It assumes that the UIDLs will be returned by the POP3 server ordered by date such that the 1st email in the UIDL list is the oldest, and the last email is the newest.

Download Chilkat Email ActiveX for POP3 / SMTP

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    --  The mailman object is used for receiving (POP3)
    --  and sending (SMTP) email.
    DECLARE @mailman int
    EXEC @hr = sp_OACreate 'Chilkat.MailMan2', @mailman OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Any string argument automatically begins the 30-day trial.
    DECLARE @success int

    EXEC sp_OAMethod @mailman, 'UnlockComponent', @success OUT, '30-day trial'
    IF @success <> 1
      BEGIN
        PRINT 'Component unlock failed'
        RETURN
      END

    --  Set the POP3 server's hostname
    EXEC sp_OASetProperty @mailman, 'MailHost', 'mail.chilkatsoft.com'

    --  Set the POP3 login/password.
    EXEC sp_OASetProperty @mailman, 'PopUsername', 'admin@chilkatsoft.com'
    EXEC sp_OASetProperty @mailman, 'PopPassword', '****'

    DECLARE @saUidls int

    --  Get the complete list of UIDLs
    EXEC sp_OAMethod @mailman, 'GetUidls', @saUidls OUT

    IF @saUidls Is NULL 
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    --  Get the 10 most recent UIDLs
    --  The 1st email is the oldest, the last email is the newest
    --  (usually)
    DECLARE @i int

    DECLARE @n int

    DECLARE @startIdx int

    EXEC sp_OAGetProperty @saUidls, 'Count', @n OUT

    IF @n > 10
      BEGIN
        SELECT @startIdx = @n - 10
      END
    ELSE
      BEGIN
        SELECT @startIdx = 0
      END

    DECLARE @saUidls2 int
    EXEC @hr = sp_OACreate 'Chilkat.CkStringArray', @saUidls2 OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    SELECT @i = @startIdx
    WHILE @i <= @n - 1
      BEGIN
        EXEC sp_OAMethod @saUidls, 'GetString', @sTmp0 OUT, @i        EXEC sp_OAMethod @saUidls2, 'Append', NULL, @sTmp0
        SELECT @i = @i + 1
      END

    --  Download in full the 10 most recent emails:
    DECLARE @bundle int

    EXEC sp_OAMethod @mailman, 'FetchMultiple', @bundle OUT, @saUidls2
    IF @bundle Is NULL 
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    DECLARE @email int

    EXEC sp_OAGetProperty @bundle, 'MessageCount', @iTmp0 OUT
    SELECT @i = 0
    WHILE @i <= @iTmp0 - 1
      BEGIN
        EXEC sp_OAMethod @bundle, 'GetEmail', @email OUT, @i
        EXEC sp_OAGetProperty @email, 'From', @sTmp0 OUT

        PRINT @sTmp0
        EXEC sp_OAGetProperty @email, 'Subject', @sTmp0 OUT

        PRINT @sTmp0

        PRINT '----'

        SELECT @i = @i + 1
      END
END
GO

 

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