Sample code for 30+ languages & platforms
SQL Server

Get a Header Attribute of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachmentAttr method, which returns a header-field attribute value from a header field of the Nth attachment. The first argument is the zero-based attachment index, the second names a header field, and the third names an attribute within it. This example reads the filename attribute of the attachment's Content-Disposition header.

Background: A MIME header can have named parameters after its main value, such as Content-Type: text/plain; charset="utf-8"; name="notes.txt". Here charset and name are attributes. GetAttachmentAttr pulls out one named attribute from a chosen header of a specific attachment, sparing you from parsing the raw header string and dealing with quoting or ordering.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    --  Demonstrates the GetAttachmentAttr method, which returns a header-field attribute value
    --  from a header field of the Nth attachment.  The first argument is the zero-based attachment
    --  index, the second is the header field name, and the third is the attribute name.

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

    EXEC sp_OASetProperty @email, 'Subject', 'Attachment header attribute'

    DECLARE @success int
    EXEC sp_OAMethod @email, 'AddStringAttachment', @success OUT, 'notes.txt', 'Some notes.'

    --  Get the "filename" attribute of the attachment's Content-Disposition header (index 0).
    DECLARE @fname nvarchar(4000)
    EXEC sp_OAMethod @email, 'GetAttachmentAttr', @fname OUT, 0, 'Content-Disposition', 'filename'

    PRINT 'Attachment filename attribute: ' + @fname

    EXEC @hr = sp_OADestroy @email


END
GO