Sample code for 30+ languages & platforms
SQL Server

Send Pre-Built MIME via SMTP

See more SMTP Examples

Demonstrates the Chilkat MailMan.SendMime method, which sends an email from caller-supplied MIME text. The arguments are the from address, the recipient list (the SMTP envelope recipients), and the MIME source. This example builds an email, gets its MIME, and sends that MIME.

Important: When sending caller-supplied MIME, ensure the Message-ID header is unique for each message. Sending MIME whose Message-ID was previously used can cause the message to be silently discarded as a duplicate by mail servers — see chilkatsoft.com/email_duplicate_message_id.asp.

Background: Sometimes you already have a complete message as raw MIME — loaded from a .eml file, generated elsewhere, or archived earlier — and just need to deliver it as-is. SendMime hands that MIME straight to the SMTP server. Note the distinction between the envelope recipients (the second argument, who the server actually delivers to) and the To/Cc headers inside the MIME (what the recipient sees) — they can legitimately differ, which is how Bcc works.

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 MailMan.SendMime method, which sends an email from caller-supplied MIME
    --  text.  The arguments are the from address, the recipient list (the SMTP envelope
    --  recipients), and the MIME source.

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

    --  Configure the SMTP server connection.
    EXEC sp_OASetProperty @mailman, 'SmtpHost', 'smtp.example.com'
    EXEC sp_OASetProperty @mailman, 'SmtpPort', 465
    EXEC sp_OASetProperty @mailman, 'SmtpSsl', 1
    EXEC sp_OASetProperty @mailman, 'SmtpUsername', 'user@example.com'
    EXEC sp_OASetProperty @mailman, 'SmtpPassword', 'myPassword'

    --  Obtain the MIME to send.  Here we build an Email and get its MIME, but the MIME could
    --  come from any source.
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

    EXEC sp_OASetProperty @email, 'Subject', 'Test email'
    EXEC sp_OASetProperty @email, 'From', 'alice@example.com'
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Bob', 'bob@example.com'
    EXEC sp_OASetProperty @email, 'Body', 'Hello, this message was sent as MIME.'
    DECLARE @mimeText nvarchar(4000)
    EXEC sp_OAMethod @email, 'GetMime', @mimeText OUT

    --  IMPORTANT: When sending caller-supplied MIME, make sure the Message-ID header is unique
    --  for each message.  Sending MIME that contains a Message-ID that was previously sent can
    --  cause the message to be silently discarded as a duplicate by mail servers.  For details,
    --  see: https://www.chilkatsoft.com/email_duplicate_message_id.asp

    --  Send the MIME to the envelope recipient(s).

    EXEC sp_OAMethod @mailman, 'SendMime', @success OUT, 'alice@example.com', 'bob@example.com', @mimeText
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @email
        RETURN
      END


    PRINT 'MIME email sent.'

    --  Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
    --  connects and authenticates -- using the property settings above -- whenever a server
    --  operation requires it.  Calling the explicit connect/authenticate methods can still be
    --  helpful to determine whether a failure occurs while connecting or while authenticating.

    EXEC @hr = sp_OADestroy @mailman
    EXEC @hr = sp_OADestroy @email


END
GO