Sample code for 30+ languages & platforms
SQL Server

Get the Content-Type of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachmentContentType method, which returns the Content-Type header field for the Nth attachment. Attachment indexing begins at 0. This example adds an attachment and reads its content type.

Background: The Content-Type (MIME type) of an attachment — such as text/plain, application/pdf, or image/png — tells a mail client how to handle the part: whether to display it inline, offer it as a download, or pick an icon for it. Reading it lets your program filter or route attachments by type, for example extracting only the PDFs from a batch of messages.

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 GetAttachmentContentType method, which returns the Content-Type header
    --  field for the Nth attachment.  The index is zero-based.

    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 content type'

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

    --  Get the Content-Type of the first attachment (index 0).
    DECLARE @ct nvarchar(4000)
    EXEC sp_OAMethod @email, 'GetAttachmentContentType', @ct OUT, 0

    PRINT 'Attachment 0 Content-Type: ' + @ct

    EXEC @hr = sp_OADestroy @email


END
GO