Sample code for 30+ languages & platforms
SQL Server

Load an Email from MIME Text in a String

See more Email Object Examples

Demonstrates the Chilkat Email.SetFromMimeText method, which loads the email from MIME text (such as the contents of a .eml file) held in a string. On success, it replaces the entire current email. This example serializes one email to MIME with GetMime, then reconstructs it in a second object.

Background: This is the in-memory (string) counterpart to LoadEml, which reads MIME from a file. It is handy when the raw message text arrives from somewhere other than the filesystem — a database column, an HTTP response, or a message queue — letting you parse it straight into a fully populated Email object.

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
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the SetFromMimeText method, which loads the email from MIME text (such as
    --  the contents of a .eml file) held in a string.  On success, it replaces the entire
    --  current email.

    --  Build a source email and get its MIME text.
    DECLARE @source int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @source OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @source, 'Subject', 'Source message'
    EXEC sp_OASetProperty @source, 'From', 'alice@example.com'
    EXEC sp_OASetProperty @source, 'Body', 'Hello from MIME text.'
    DECLARE @mimeText nvarchar(4000)
    EXEC sp_OAMethod @source, 'GetMime', @mimeText OUT

    --  Load a new email object from the MIME text.
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

    EXEC sp_OAMethod @email, 'SetFromMimeText', @success OUT, @mimeText
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @source
        EXEC @hr = sp_OADestroy @email
        RETURN
      END


    EXEC sp_OAGetProperty @email, 'Subject', @sTmp0 OUT
    PRINT 'Loaded subject: ' + @sTmp0

    EXEC @hr = sp_OADestroy @source
    EXEC @hr = sp_OADestroy @email


END
GO