SQL Server
SQL Server
Get Accepted and Rejected SMTP Recipients
See more SMTP Examples
Demonstrates the Chilkat MailMan.SmtpRecipientsLog method, which returns the list of recipient email addresses accepted or rejected by the SMTP server during the most recent email-send operation. Pass true to get the rejected list or false to get the accepted list. This example sends a message to two recipients and reports both lists.
Background: A send to multiple recipients is not necessarily all-or-nothing: the server judges each
RCPT TO separately, so a message can be delivered to some addresses while others are refused. Inspecting these lists after a send tells you exactly who was accepted and who was not — essential for reporting partial failures accurately or flagging bad addresses in a mailing list, rather than assuming a successful send reached everyone.Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Demonstrates the MailMan.SmtpRecipientsLog method, which returns the list of recipient
-- email addresses accepted or rejected by the SMTP server during the most recent email-send
-- operation. Pass 1 for the rejected list, or 0 for the accepted list.
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'
-- Send an email to two recipients.
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_OAMethod @email, 'AddTo', @success OUT, 'Carol', 'carol@example.com'
EXEC sp_OASetProperty @email, 'Body', 'Hello!'
EXEC sp_OAMethod @mailman, 'SendEmail', @success OUT, @email
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
-- Get the recipients the server accepted (rejected = 0).
DECLARE @accepted int
EXEC @hr = sp_OACreate 'Chilkat.StringTable', @accepted OUT
EXEC sp_OAMethod @mailman, 'SmtpRecipientsLog', NULL, 0, @accepted
EXEC sp_OAGetProperty @accepted, 'Count', @iTmp0 OUT
PRINT 'Accepted recipients: ' + @iTmp0
-- Get the recipients the server rejected (rejected = 1).
DECLARE @rejected int
EXEC @hr = sp_OACreate 'Chilkat.StringTable', @rejected OUT
EXEC sp_OAMethod @mailman, 'SmtpRecipientsLog', NULL, 1, @rejected
DECLARE @n int
EXEC sp_OAGetProperty @rejected, 'Count', @n OUT
PRINT 'Rejected recipients: ' + @n
DECLARE @i int
SELECT @i = 0
WHILE @i <= @n - 1
BEGIN
EXEC sp_OAMethod @rejected, 'StringAt', @sTmp0 OUT, @i
PRINT @sTmp0
SELECT @i = @i + 1
END
-- 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
EXEC @hr = sp_OADestroy @accepted
EXEC @hr = sp_OADestroy @rejected
END
GO