Sample code for 30+ languages & platforms
SQL Server

Send MIME Bytes from a BinData via SMTP

See more SMTP Examples

Demonstrates the Chilkat MailMan.SendMimeBd method, which sends an email from caller-supplied MIME bytes held in a BinData. The arguments are the from address, the recipient list (the SMTP envelope recipients), and the BinData containing the MIME. This example loads a .eml file into a BinData and sends it.

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: This is the binary counterpart to SendMime (which takes MIME as a string). Working from a BinData is the right choice when the message's raw bytes come straight from a file, a database blob, or a network stream — it delivers them byte-for-byte with no charset conversion that could alter the original, which matters because MIME can carry binary transfer encodings.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the MailMan.SendMimeBd method, which sends an email from caller-supplied MIME
    --  bytes held in a BinData.  The arguments are the from address, the recipient list (the SMTP
    --  envelope recipients), and the BinData containing the MIME.

    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'

    --  Load the MIME bytes to send.  Here we read a .eml file, but the bytes could come from
    --  any source.
    DECLARE @bdMime int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdMime OUT

    EXEC sp_OAMethod @bdMime, 'LoadFile', @success OUT, 'qa_data/eml/message.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @bdMime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @bdMime
        RETURN
      END

    --  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 bytes to the envelope recipient(s).
    EXEC sp_OAMethod @mailman, 'SendMimeBd', @success OUT, 'alice@example.com', 'bob@example.com', @bdMime
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @bdMime
        RETURN
      END


    PRINT 'MIME email sent.'

    --  Note: The path "qa_data/eml/message.eml" is a relative local filesystem path,
    --  relative to the current working directory of the running application.

    --  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 @bdMime


END
GO