Hello,
I'm using the 64bit ActiveX control (version 9.5.0.59) to send emails to an Office365 SMTP server from SQL Server 2012. It all works well except that the To address is not cleared out after each Send(). I'm creating a single instance of the MailMan and Email objects and just re-populating the From, To, Subject and Message, see the sample code below.
DECLARE @lQuit bit
SET @lQuit = 0
DECLARE @hr int
DECLARE @sErrorMsg nvarchar(4000)
-- The mailman object is used for sending and receiving email.
DECLARE @mailman int
EXEC @hr = sp_OACreate 'Chilkat_9_5_0.MailMan', @mailman OUT
IF @hr<> 0
BEGIN
RAISERROR('Unable To create mail control.',16,1)
RETURN
END
-- unlock the control
DECLARE @success int
EXEC sp_OAMethod @mailman, 'UnlockComponent', @success OUT, 'xxxxxxxxx'
IF STR(@success) <> 1
BEGIN
EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sErrorMsg OUT
RAISERROR(@sErrorMsg,16,2)
EXEC @hr = sp_OADestroy @mailman
RETURN
END
-- set SMTP settings
EXEC sp_OASetProperty @mailman, 'SmtpHost', 'smtp.office365.com'
EXEC sp_OASetProperty @mailman, 'SmtpPort', 587
EXEC sp_OASetProperty @mailman, 'StartTLS', 1
-- Set the SMTP login/password
EXEC sp_OASetProperty @mailman, 'SmtpUsername', 'user_name'
EXEC sp_OASetProperty @mailman, 'SmtpPassword', 'password'
-- Create an email object
DECLARE @email int
EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Email', @email OUT
IF @hr<> 0
BEGIN
RAISERROR('Unable To create the email object.',16,3)
EXEC @hr = sp_OADestroy @mailman
RETURN
END
-- send the first email
EXEC sp_OAMethod @email, 'ClearTo', @success OUT
EXEC sp_OAMethod @email, 'RemoveHtmlAlternative', @success OUT
EXEC sp_OAMethod @email, 'RemovePlainTextAlternative', @success OUT
-- code to send out the email
EXEC sp_OASetProperty @email, 'Subject', 'This is the first email'
EXEC sp_OAMethod @email, 'SetHtmlBody', NULL, '<html><body>1. this is the HTML body</body></html>'
EXEC sp_OAMethod @email, 'AddPlainTextAlternativeBody', @success OUT, 'this is the plain text version of the body'
EXEC sp_OASetProperty @email, 'From', 'from_email'
EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'support', 'email_address1'
EXEC sp_OAMethod @mailman, 'SendEmail', @success OUT, @email
-- send the second email
EXEC sp_OAMethod @email, 'ClearTo', @success OUT
EXEC sp_OAMethod @email, 'RemoveHtmlAlternative', @success OUT
EXEC sp_OAMethod @email, 'RemovePlainTextAlternative', @success OUT
-- code to send out the email
EXEC sp_OASetProperty @email, 'Subject', 'This is the second email'
EXEC sp_OAMethod @email, 'SetHtmlBody', NULL, '<html><body>2. this is the HTML body</body></html>'
EXEC sp_OAMethod @email, 'AddPlainTextAlternativeBody', @success OUT, 'this is the plain text version of the body'
EXEC sp_OASetProperty @email, 'From', 'from_email'
EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'support', 'email_address2'
EXEC sp_OAMethod @mailman, 'SendEmail', @success OUT, @email
-- send the third email
EXEC sp_OAMethod @email, 'ClearTo', @success OUT
EXEC sp_OAMethod @email, 'RemoveHtmlAlternative', @success OUT
EXEC sp_OAMethod @email, 'RemovePlainTextAlternative', @success OUT
-- code to send out the email
EXEC sp_OASetProperty @email, 'Subject', 'This is the third email'
EXEC sp_OAMethod @email, 'SetHtmlBody', NULL, '<html><body>3. this is the HTML body</body></html>'
EXEC sp_OAMethod @email, 'AddPlainTextAlternativeBody', @success OUT, 'this is the plain text version of the body'
EXEC sp_OASetProperty @email, 'From', 'from_email'
EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'support', 'email_address3'
EXEC sp_OAMethod @mailman, 'SendEmail', @success OUT, @email
IF STR(@success) <> 1
BEGIN
-- got an error
EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sErrorMsg OUT
SET @sErrorMsg = 'Unable to send the email';
RAISERROR(@sErrorMsg,16,4)
SET @lQuit = 1
END
EXEC @hr = sp_OADestroy @mailman
EXEC @hr = sp_OADestroy @email
When i execute the code from SSMS, it sends out 3 emails, however i see from Outlook that the first email has one To address, the second email has two To addresses, and the third email has all three To addresses. So it appears that the ClearTo() is not working. No errors are returned by the LastErrorText() method when placed right after the ClearTo() method.
Thanks for developing a great product! it generally works flawlessly!!