SQL Server
SQL Server
Create Non-Multipart Email with XML Body that is an Attachment
See more SMTP Examples
Creates an email where the only body is a binary WAV file. The technique used in the example could be applied to other binary files, such as PDF, MS-WORD docs, Excel docs, etc.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- The goal of this example is to create an email that has this MIME format,
-- where the body of the email is XML using base64 encoding,
-- and the Content-Type and Content-Disposition are set as indicated.
-- Date: Wed, 23 Aug 2023 09:01:52 +0200 (CEST)
-- From: joe@example1.com
-- Subject: [v=EMCS.NL][a=NL****][k=****][s=0]
-- To: mary@example2.com
-- MIME-Version: 1.0
-- Content-Type: application/octet-stream; charset=us-ascii; name=6bd8dfe259b3a42c90a28a82b5fc6e77.txt
-- Content-Transfer-Encoding: Base64
-- Content-Disposition: attachment; filename=6bd8dfe259b3a42c90a28a82b5fc6e77.txt
-- Message-Id: <......>
--
-- <multi-line base64 encoded XML here>
SELECT @success = 1
DECLARE @xmlBody nvarchar(4000)
SELECT @xmlBody = '<ie:IE801 xmlns:ie="urn:publicid:-:EC:DGTAXUD:EMCS:PHASE4:IE801:V3.01" xmlns:tms="urn:publicid:-:EC:DGTAXUD:EMCS:PHASE4:TMS:V3.01">....</ie:IE801>'
DECLARE @email int
EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Add subject, TO, FROM, etc.
EXEC sp_OASetProperty @email, 'Subject', '[v=EMCS.NL][a=NL****][k=****][s=0]'
EXEC sp_OASetProperty @email, 'From', 'joe@example1.com'
EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Mary', 'mary@example2.com'
EXEC sp_OASetProperty @email, 'Body', @xmlBody
EXEC sp_OAMethod @email, 'AddHeaderField', NULL, 'Content-Transfer-Encoding', 'Base64'
EXEC sp_OAMethod @email, 'AddHeaderField', NULL, 'Content-Type', 'application/octet-stream; charset=us-ascii; name=6bd8dfe259b3a42c90a28a82b5fc6e77.txt'
EXEC sp_OAMethod @email, 'AddHeaderField', NULL, 'Content-Disposition', 'attachment; filename=6bd8dfe259b3a42c90a28a82b5fc6e77.txt'
-- Your email is ready to send
DECLARE @mailman int
EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mailman OUT
EXEC sp_OASetProperty @mailman, 'SmtpHost', 'smtp.my_mail_server.com'
EXEC sp_OASetProperty @mailman, 'SmtpUsername', 'myUsername'
EXEC sp_OASetProperty @mailman, 'SmtpPassword', 'myPassword'
EXEC sp_OASetProperty @mailman, 'SmtpPort', 465
EXEC sp_OASetProperty @mailman, 'SmtpSsl', 1
EXEC sp_OASetProperty @mailman, 'StartTLS', 1
EXEC sp_OAMethod @mailman, 'SendEmail', @success OUT, @email
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @mailman
RETURN
END
EXEC sp_OAMethod @mailman, 'CloseSmtpConnection', @success OUT
IF @success <> 1
BEGIN
PRINT 'Connection to SMTP server not closed cleanly.'
END
PRINT 'Mail Sent!'
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @mailman
END
GO