Data Management

How do I configure SQL mail in SQL Server 2000?

SQL Mail is a component of SQL Server that allows you to send mail. Some of its functions include the ability to send messages to an e-mail pager and to send results via the extended stored procedure (xp_sendmail).

As a database administrator, I like automation. When it comes to my backups, I like to automate as much as possible. SQL Mail gives me the ability to know when my backups are successful and when they fail.

SQL Mail is a component of SQL Server that allows you to send mail. Some of its functions include the ability to send messages to an e-mail pager and to send results via the extended stored procedure (xp_sendmail).

SQL Mail allows you to send and receive e-mail by working side by side with a mail server. There are two services that handle SQL Mail with SQL Server 2000: MSSQLServer and SQLServerAgent. I am going to explain how to configure and take advantage of this hidden gem.

Setup

Before you configure SQL Mail, you will need to configure a mailbox, mail profile, and a Windows 2000 account to start SQL Server. If you are using Exchange, you need a domain account. If you are using a basic POP3/SMTP mail server, you need a local or domain account. For the purposes of this article, I will show you how to configure a POP3/SMTP mail server.

You will begin by creating an account on your domain that will be used to configure SQL Mail, as shown in Figure A. In order to configure your Microsoft Outlook client, you first need to make sure you have Microsoft Outlook installed. You can install this client from the Microsoft Office CD. Once you have it installed, click Control Panel from the Start Menu, then double-click the Mail icon, as shown in Figure B. Next, click Show Profiles, and then click Add, to add a new profile, as shown in Figure C. You will now be prompted with a wizard to add a new e-mail account, as shown in Figure D. Once you complete this step with the appropriate information, you are ready to configure SQL Mail.

Next, you need to log in to Windows with the newly created account. Once you are logged in, your next step is to start your MSSQLServer service and SQLServerAgent service.

In order to configure your MSSQLServer account to run under this newly created account, open Enterprise Manager from Start | Programs | Microsoft SQL Server | Enterprise Manager. Next, right-click on your SQL Server and choose Properties, as shown in Figure E, then choose the Security tab. Under the Startup Service Account, choose This Account and type the name of the account you created for use with SQL Mail. Now that you have configured the MSSQLServer service account, you will need to configure the SQLServerAgent service account. In order to do this, expand SQL Server and Management, then right-click on SQL Server Agent and choose Properties (Figure F). On the General Page, enter the Service Startup Account by choosing This Account and enter the account name and password you created to start your SQLServerAgent service. In order to configure your Microsoft Outlook client, you first need to make sure you have Microsoft Outlook installed. You can install this client from the Microsoft Office CD. Once you have it installed, click Control Panel from the Start Menu, then double-click the Mail icon, as shown in Figure G. Next, click Show Profiles, and then click Add, to add a new profile, as shown in Figure H. You will now be prompted with a wizard to add a new e-mail account, as shown in Figure I. Once you complete this step with the appropriate information, you are ready to configure SQL Mail.

Next, we will show you how to Configure SQL Mail now that you have configured the Outlook client.

Configure SQL Mail

In order to configure SQL Mail, you must open the SQL Server Enterprise Manager from the Start menu, then explore your SQL Server and expand your Support Services folder, as shown in Figure J. Now right-click on Support Services and choose Properties. From the dropdown menu, choose the profile you just created (Figure K).

If for some reason, you do not see the profile you created in the dropdown menu, you have configured your Outlook mail settings incorrectly. Please go back and check your work for something that you may have missed.

Click Test to verify that your settings work correctly (Figure L). The test starts and stops the MAPI profile you created. Our next test is to create a SQL Server Operator and test the SQL Mail functionality. To create an Operator, expand SQL Server | Management | SQL Server Agent | Operators and highlight Operators, as shown in Figure M. Next, right-click on Operators and choose New Operator (Figure N). Now, type the name of the operator and the operator's e-mail address for SQL Mail to use. Next, click Test to send a test e-mail (Figure O). A dialog box will display a message that you have sent your e-mail successfully. Your next step is to open up Outlook and check your e-mail to see that you are receiving these messages, as shown in Figure P. Now that you have configured SQL Mail, you can take advantage of the xp_sendmail stored procedure that allows you to send messages through T-SQL. In my example, I am going to send an e-mail that will tell me which version of SQL Server I currently have running. Figure Q shows how this would break out.

The parameters for using xp_sendmail are as follows:

xp_sendmail {[@recipients=] 'recipients [;...n]'}

[,[@message=] 'message']

[,[@query=] 'query']

[,[@attachments=] 'attachments [;...n]']

[,[@copy_recipients=] 'copy_recipients [;...n]'

[,[@blind_copy_recipients=] 'blind_copy_recipients [;...n]'

[,[@subject=] 'subject']

[,[@type=] 'type']

[,[@attach_results=] 'attach_value']

[,[@no_output=] 'output_value']

[,[@no_header=] 'header_value']

[,[@width=] width]

[,[@separator=] 'separator']

[,[@echo_error=] 'echo_value']

[,[@set_user=] 'user']

[,[@dbuse=] 'database']

With the above method, you can create triggers in your SQL code to notify administrators, including yourself, via e-mail if certain conditions occur. For example, you might set up notifications for long running queries, the deletion of certain tables, the rebuilding of indexes, backups failing, and a host of other database-related inquiries.

As you can see, SQL Mail can be very powerful. I have introduced you to the possibilities of SQL Mail and how to incorporate it into your infrastructure. In addition, I walked you through the process of configuring SQL Mail and testing it to make sure the necessary pieces work.

Your next step will be to continue testing with various options, and then begin using this solution in a production environment to automatically notify you of any SQL Server problems, issues, or disasters.

Stay tuned for how SQL Mail has changed in SQL Server 2005/2008 with all new tutorials.

3 comments
jdhart123
jdhart123

I know it's been several years since you posted this but I want to thank you, nonetheless. I spent two days working on SQL triggers and email notifications. I was working on SQL Server 2005/2008. I learned the hard way that the same stored procedures aren't present in SQL 2000. This article was very thorough and listed everything step-by-step. Thank you!

cvnrao_us
cvnrao_us

THIS IS really very nice article, giving the details of mail setup from the scratch... Really very good document.. CVN RAO

thulasirm
thulasirm

HI Team, Whenever i am configuring mail i need to provied service account name but my system is having system account.Now i am changing to my outlook profile account.At that time its showing that your server will restart do this this will reflct the remaing machines.What is the solution for this? Please can any one suggest on the same/ Thanks Thulasi R

Editor's Picks