Sample code for 30+ languages & platforms
SQL Server

Get the Content-ID of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachmentContentID method, which returns the Content-ID header field for the Nth attachment. The index is zero-based, so the first attachment is 0. This example adds an attachment and reads its Content-ID (which may be empty when none was assigned).

Background: A Content-ID is a unique identifier for a MIME part. It is essential for inline "related" resources, where HTML references an image by cid:, but ordinary file attachments usually have none. Reading it is useful when inspecting how a message is assembled, or when distinguishing parts that are referenced from the body versus those meant purely as downloadable attachments.

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 GetAttachmentContentID method, which returns the Content-ID header
    --  field for the Nth attachment.  The index is zero-based (the first attachment is 0).

    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-ID'

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

    --  Get the Content-ID of the first attachment (may be empty if none was assigned).
    DECLARE @cid nvarchar(4000)
    EXEC sp_OAMethod @email, 'GetAttachmentContentID', @cid OUT, 0

    PRINT 'Attachment 0 Content-ID: ' + @cid

    EXEC @hr = sp_OADestroy @email


END
GO