Sunday, August 13, 2017

Send Email Using SQL Server

In this article I will explain how to send email using SQL server. For this you need to have SQL server enterprise edition. It uses SMTP server to communicate with user.

Following are step to send Email using SQL Server.

Step 1:

You need to configure Profile and Account in 'Database Mail' under 'Management' node .



Click on 'Configure Database Mail' will open below wizard.











If Profile is configured for first time, click on 'Yes'







Set Profile Name and Description. Click on add button to create Database Mail account.












Click Next.









Click on finish will set up account and profile.





Close wizard. Now you can send a test mail.



You will get email in your Inbox.


Step 2:

After creating mail profile and testing of mail, you can write query for executing mail by simply passing parameters to stored procedure called 'sp_send_dbmail'. This stored procedure is in the msdb database. Following is example.


declare @body1 varchar(4000)
set @body1 = 'This is test mail sent from SQQL 2008  '

EXEC msdb.dbo.sp_send_dbmail
    @profile_name='MailTutorial',
    @recipients='isalmankavish@gmail.com',
    @subject = 'Mail Tutorial',
    @body = @body1,
    @body_format = 'HTML',

Here you have different option such as attaching file in an email. also you can schedule above query to send mail at particular interval of time.

for further study about 'sp_send_dbmail' you can visit to MSDN website.
https://msdn.microsoft.com/en-IN/library/ms190307.aspx

No comments:

Post a Comment