Automate with SQL Mail

Friday Oct 22nd 2004 by Steven Warren

As a database administrator, I like automation. Learn how you can create triggers in your SQL code to notify administrators, including yourself, via e-mail if certain conditions occur.

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.


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.

Figure A

Set up a service account for 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 B, 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.

Figure B

Set up MSSQLServer to run under the service account.

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 C). 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.

Figure C

Set up SQLServerAgent to run under the service account.

Configure the Outlook Client

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 D.

Figure D

You are now ready to configure a mail account.

Next, click Show Profiles, and then click Add, to add a new profile, as shown in Figure E.

Figure E

Configure your mail profile.

You will now be prompted with a wizard to add a new e-mail account, as shown in Figure F. Once you complete this step with the appropriate information, you are ready to configure SQL Mail.

Figure F

Use Outlook to set up the actual mail account.

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 G.

Figure G
Click for larger image

Open the SQL Server Enterprise Manager.

Now right-click on Support Services and choose Properties. From the dropdown menu, choose the profile you just created (Figure H).

Figure H
Click for larger image

Select your mail profile.

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 I). The test starts and stops the MAPI profile you created.

Figure I

Take advantage of the Test feature to check your profile.

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 J.

Figure J

Here's where you set up a SQL Server operator.

Next, right-click on Operators and choose New Operator (Figure K).

Figure K

Create a new operator.

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 L). A dialog box will display a message that you have sent your e-mail successfully.

Figure L

Run a test on sending e-mail from SQL Mail.

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 M.

Figure M

Go to your e-mail client to verify that the SQL Mail messages are being delivered.

Configure xp_sendmail

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 N shows how this would break out.

Figure N

Using xp_sendmail allows you to mail SQL Server information.

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.

» See All Articles by Columnist Steven S. Warren

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved