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 Encryption
Socket
Spider
String
Tar
Upload
XML
XMP
Zip

Byte Array
RSS
Atom
Self-Extractor

Save an Email Attachment using a Specific Filename

Demonstrates how to save an email attachment using a specific filename, regardless of the filename in the email.

Download Chilkat Email ActiveX for POP3 / SMTP

Download Chilkat IMAP ActiveX

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    --  Create a MailMan or Imap object first for the purpose
    --  of unlocking the component. (Only one is necessary)
    DECLARE @mailman int
    EXEC @hr = sp_OACreate 'Chilkat.MailMan2', @mailman OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @mailman, 'UnlockComponent', NULL, 'anything for 30-day trial'
    DECLARE @imap int
    EXEC @hr = sp_OACreate 'Chilkat.Imap', @imap OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @imap, 'UnlockComponent', NULL, 'anything for 30-day trial'

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

    --  Load an email object containing attachments.
    --  This .eml can be downloaded from:
    --  http://www.example-code.com/testData/HtmlEmail.eml
    DECLARE @success int

    EXEC sp_OAMethod @email, 'LoadEml', @success OUT, 'HtmlEmail.eml'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    --  List the attachment filenames:
    --  This email has two attachments: logo.jpg and faxCover.doc
    DECLARE @i int

    EXEC sp_OAGetProperty @email, 'NumAttachments', @iTmp0 OUT
    @i = 0
    WHILE @i < @iTmp0 - 1
      BEGIN
        EXEC sp_OAMethod @email, 'GetAttachmentFilename', @sTmp0 OUT, @i        PRINT @sTmp0
        @i = @i + 1
      END

    --  Let's say we want to save the 1st attachment as "pic.jpg",
    --  and the 2nd attachment as "cover.doc".
    --  Call SetAttachmentFilename for both:
    EXEC sp_OAMethod @email, 'SetAttachmentFilename', NULL, 0, 'pic.jpg'
    EXEC sp_OAMethod @email, 'SetAttachmentFilename', NULL, 1, 'cover.doc'

    --  The SaveAttachedFile method allows you to specify the directory
    --  where the attachment is saved.  To save to the current working
    --  directory, pass a "." for the directory path.  This code
    --  saves each attachment to the current working directory:
    EXEC sp_OAMethod @email, 'SaveAttachedFile', NULL, 0, '.'
    EXEC sp_OAMethod @email, 'SaveAttachedFile', NULL, 1, '.'

    --  Alternatively, we could save all the attachments in a single
    --  call.  The argument to SaveAllAttachments is the directory
    --  path.  In this case, the filenames set by SetAttachmentFilename
    --  are used:
    EXEC sp_OAMethod @email, 'SaveAllAttachments', NULL, 'myAttachments'

    PRINT 'Finished!'

END
GO

 

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

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