SQL Server Stored Procedure Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

SQL Server
Stored Procedure Examples

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

Bz2
CSV
FileAccess
Byte Array
RSS
Atom
Self-Extractor

Load MHT and Send as Email

Load a .MHT file and send it as email.

Download Chilkat Email ActiveX for POP3 / SMTP

Download Chilkat IMAP ActiveX

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    --  The mailman object is used for sending and receiving 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 SMTP server.
    EXEC sp_OASetProperty @mailman, 'SmtpHost', 'smtp.comcast.net'

    --  Create a new email object
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email2', @email OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  MHT is MIME and therefore can be loaded directly
    --  into an email object via the email.LoadEml method.
    --  However, if the HTML sub-part of the MIME contains
    --  header fields such as this:

    --  Content-ID: <BalanceSheet>
    --  Content-Disposition: inline;
    --  	filename="BalanceSheet"
    --  Content-Type: text/html;
    --  	name="BalanceSheet";
    --  	charset="utf-8"

    --  Microsoft Outlook will interpret this as an attached HTML file
    --  rather than the HTML body of the email.  Interestingly,
    --  Mozilla Thunderbird handles it correctly, but Outlook does not.
    --  Google's GMail also handles it correctly.  Yahoo Mail doesn't
    --  know what to do and displays nothing -- not even the "attachment".

    --  Rather than loading the MHT into the email object, we'll
    --  first load it into a Chilkat MIME object and check for
    --  these headers.  We want the header fields of the HTML sub-part
    --  to look like this:

    --  Content-Type: text/html;
    --  	charset="utf-8"

    --  To do so, we'll remove the Content-ID and Content-Disposition
    --  header fields, and the "name" attribute from the
    --  Content-Type header.

    --  Note: Chilkat MIME is a separate product.  This code
    --  would require licenses to both Chilkat Email and Chilkat Mime,
    --  or alternatively a Chilkat Bundle license (which includes
    --  all Chilkat components).

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

    EXEC sp_OAMethod @mime, 'UnlockComponent', @success OUT, 'Anything for 30-day trial'
    IF @success <> 1
      BEGIN
        PRINT 'Failed to unlock MIME component'
        RETURN
      END

    EXEC sp_OAMethod @mime, 'LoadMimeFile', @success OUT, 'test.mht'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    --  Is this a multipart/related?  If so, find the HTML part,
    --  which should be the 1st sub-part.
    EXEC sp_OAMethod @mime, 'IsMultipartRelated', @sTmp0 OUT    IF @sTmp0 = 1
      BEGIN
        --  Find the HTML part.
        DECLARE @mimePart int

        DECLARE @n int

        DECLARE @i int

        EXEC sp_OAGetProperty @mime, 'NumParts', @n OUT

        SELECT @i = 0
        WHILE @i <= @n - 1
          BEGIN
            EXEC sp_OAMethod @mime, 'GetPart', @mimePart OUT, @i
            EXEC sp_OAMethod @mimePart, 'IsHtml', @sTmp0 OUT            IF @sTmp0 = 1
              BEGIN
                BREAK
              END

            SELECT @i = @i + 1
          END
        IF Not (@mimePart Is NULL )
          BEGIN
            --  Remove these header fields:
            EXEC sp_OAMethod @mimePart, 'SetHeaderField', NULL, 'Content-Disposition', ''
            EXEC sp_OAMethod @mimePart, 'SetHeaderField', NULL, 'Content-ID', ''
            --  Remove the "name" attribute from the Content-Type header:
            EXEC sp_OASetProperty @mimePart, 'Name', ''

          END
      END

    EXEC sp_OAMethod @mime, 'GetMime', @sTmp0 OUT    EXEC sp_OAMethod @email, 'SetFromMimeText', NULL, @sTmp0

    EXEC sp_OASetProperty @email, 'Subject', 'This is a test'

    EXEC sp_OASetProperty @email, 'From', 'Chilkat Support <support@chilkatsoft.com>'
    EXEC sp_OAMethod @email, 'AddTo', NULL, 'Chilkat Admin', 'admin@chilkatsoft.com'

    EXEC sp_OAMethod @mailman, 'SendEmail', @success OUT, @email
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
      END
    ELSE
      BEGIN
        PRINT 'Mail Sent!'
      END
END
GO

 

Need a specific example? Send a request to support@chilkatsoft.com

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