Sample code for 30+ languages & platforms
SQL Server

Download the Full Version of a Partial Email

See more POP3 Examples

Demonstrates the Chilkat MailMan.FetchFull method, which downloads the complete version of a header-only or partial Email and stores it in a second Email. The full result includes the complete body and any attachments, and the partial email must retain its UIDL. This example previews message 1 (headers only) and then downloads it in full.

Background: A common pattern is to download headers first — cheap and fast — show the user a message list, and only pull the full content when a message is actually opened. FetchFull completes that "download on demand" flow: given a partial email (which carries its UIDL), it fetches the rest from the server. This keeps bandwidth and latency low when browsing a large mailbox.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the MailMan.FetchFull method, which downloads the complete version of a
    --  header-only or partial Email and stores it in a second Email.  The full result includes
    --  the complete body and any attachments.  The partial email must retain its UIDL.

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

    --  Configure the POP3 server connection.
    EXEC sp_OASetProperty @mailman, 'MailHost', 'pop.example.com'
    EXEC sp_OASetProperty @mailman, 'MailPort', 995
    EXEC sp_OASetProperty @mailman, 'PopSsl', 1
    EXEC sp_OASetProperty @mailman, 'PopUsername', 'user@example.com'
    EXEC sp_OASetProperty @mailman, 'PopPassword', 'myPassword'

    --  First fetch message 1 as headers only (a fast preview).  headerOnly=1.
    DECLARE @partialEmail int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @partialEmail OUT

    EXEC sp_OAMethod @mailman, 'FetchOne', @success OUT, 1, 0, 1, @partialEmail
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @partialEmail
        RETURN
      END

    EXEC sp_OAGetProperty @partialEmail, 'Subject', @sTmp0 OUT
    PRINT 'Preview subject: ' + @sTmp0

    --  Now download the complete message (body and attachments).
    DECLARE @fullEmail int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @fullEmail OUT

    EXEC sp_OAMethod @mailman, 'FetchFull', @success OUT, @partialEmail, @fullEmail
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @partialEmail
        EXEC @hr = sp_OADestroy @fullEmail
        RETURN
      END

    EXEC sp_OAGetProperty @fullEmail, 'NumAttachments', @iTmp0 OUT
    PRINT 'Full email NumAttachments: ' + @iTmp0

    --  Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
    --  connects and authenticates -- using the property settings above -- whenever a server
    --  operation requires it.  Calling the explicit connect/authenticate methods can still be
    --  helpful to determine whether a failure occurs while connecting or while authenticating.

    EXEC @hr = sp_OADestroy @mailman
    EXEC @hr = sp_OADestroy @partialEmail
    EXEC @hr = sp_OADestroy @fullEmail


END
GO