I want to use SQL Server to send email and found two possible way from the internet:
-
Using AOSMTP.Mail
Create PROCEDURE [dbo].[RC_SendEmail] @ServerAddr varchar(80), @FromAddr varchar(80), @Recipient varchar(80), @Subject varchar(132), @BodyText varchar(2000) AS DECLARE @hr int DECLARE @oSmtp int DECLARE @nRet int EXEC @hr = sp_OACreate 'AOSMTP.Mail',@oSmtp OUT EXEC @hr = sp_OASetProperty @oSmtp, 'RegisterKey', 'replace this text by your key' EXEC @hr = sp_OASetProperty @oSmtp, 'ServerAddr', @ServerAddr EXEC @hr = sp_OASetProperty @oSmtp, 'FromAddr', @FromAddr EXEC @hr = sp_OAMethod @oSmtp, 'AddRecipient', NULL, @Recipient, @Recipient, 0 EXEC @hr = sp_OASetProperty @oSmtp, 'Subject', @Subject EXEC @hr = sp_OASetProperty @oSmtp, 'BodyText', @BodyText EXEC @hr = sp_OAMethod @oSmtp, 'SendMail', @nRet OUT EXEC @hr = sp_OADestroy @oSmtp
However, this method required a Server Address.
What is the address I am supposed to enter? the email server? or the database server?
I only see that it use ‘localhost’ in the internet. But it doesn’t work for me.
-
Using CDONTS.NewMail
CREATE PROCEDURE [dbo].[SendEmail] @From varchar(100), @To varchar(100), @Subject varchar(100), @Body varchar(4000), @CC varchar(100) = null, @BCC varchar(100) = null AS Declare @MailID int Declare @hr int Declare @result int EXEC @hr = sp_OACreate 'CDONTS.NewMail', @MailID OUT EXEC @hr = sp_OASetProperty @MailID, 'From',@From EXEC @hr = sp_OASetProperty @MailID, 'Body', @Body EXEC @hr = sp_OASetProperty @MailID, 'BCC',@BCC EXEC @hr = sp_OASetProperty @MailID, 'CC', @CC EXEC @hr = sp_OASetProperty @MailID, 'Subject', @Subject EXEC @hr = sp_OASetProperty @MailID, 'To', @To EXEC @hr = sp_OAMethod @MailID, 'Send', @result OUT EXEC @hr = sp_OADestroy @MailID select @result
This one required no Server Address but it doesn’t work as well.
Can anyone explain in more details about these and what I should do?
Thank you very much!!!
Forget that, use the send email capabilities built-in on SQL Server. It doesn’t get any easier.
Once you configure it through the wizard, you can simply do this inside your procs, as shown in the tutorial linked: