Configure SQL Mail in SQL Server

Embed Size (px)

Citation preview

  • 7/31/2019 Configure SQL Mail in SQL Server

    1/12

    Takeaway: SQL Mail is a component of SQL Server that allows you to send mail. Some of itsfunctions include the ability to send messages to an e-mail pager and to send results via theextended 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 aresuccessful and when they fail.

    SQL Mail is a component of SQL Server that allows you to send mail. Some of its functionsinclude the ability to send messages to an e-mail pager and to send results via the extendedstored 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 andSQLServerAgent. I am going to explain how to configure and take advantage of this hiddengem.

    Setup

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

    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

  • 7/31/2019 Configure SQL Mail in SQL Server

    2/12

    Microsoft Outlook installed. You can install this client from the Microsoft Office CD. Once youhave it installed, click Control Panel from the Start Menu, then double-click the Mail icon, asshown 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.

  • 7/31/2019 Configure SQL Mail in SQL Server

    3/12

    Once you complete this step with the appropriate information, you are ready to configure SQLMail.

    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,

  • 7/31/2019 Configure SQL Mail in SQL Server

    4/12

    then choose the Security tab. Under the Startup Service Account, choose This Account andtype 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 configurethe SQLServerAgent service account. In order to do this, expand SQL Server andManagement, then right-click on SQL Server Agent and choose Properties (Figure F).

  • 7/31/2019 Configure SQL Mail in SQL Server

    5/12

    On the General Page, enter the Service Startup Account by choosing This Account and enterthe 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 haveMicrosoft Outlook installed. You can install this client from the Microsoft Office CD. Once youhave it installed, click Control Panel from the Start Menu, then double-click the Mail icon, asshown in Figure G.

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

  • 7/31/2019 Configure SQL Mail in SQL Server

    6/12

    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

  • 7/31/2019 Configure SQL Mail in SQL Server

    7/12

    client.

    Configure SQL Mail

    In order to configure SQL Mail, you must open the SQL Server Enterprise Manager from theStart 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 haveconfigured your Outlook mail settings incorrectly. Please go back and check your work forsomething that you may have missed.

    Click Test to verify that your settings work correctly (Figure L).

  • 7/31/2019 Configure SQL Mail in SQL Server

    8/12

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

  • 7/31/2019 Configure SQL Mail in SQL Server

    9/12

    Now, type the name of the operator and the operators 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 receivingthese messages, as shown in Figure P.

  • 7/31/2019 Configure SQL Mail in SQL Server

    10/12

    Now that you have configured SQL Mail, you can take advantage of the xp_sendmail storedprocedure that allows you to send messages through T-SQL. In my example, I am going tosend 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]

  • 7/31/2019 Configure SQL Mail in SQL Server

    11/12

    [,[@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]

    Un ejemplo

    DECLARE @r AS intEXEC @r = master..xp_sendmail @recipients = '',

    @message = 'Message text',@query = 'SELECT * FROM titles',@subject = 'SQL Mail test to attach query

    results',@attach_results = 'TRUE',@dbuse = 'pubs'

    execute master..xp_sendmail@recipients = 'user1',@message = 'Testing',@query = 'select * from sysobjects',

    @width = 256,@subject = 'testing',@attach_results = 'true',@no_header = 'false',@separator = ',',@attachments = 'test.csv'

    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 upnotifications 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 ofSQL Mail and how to incorporate it into your infrastructure. In addition, I walked you throughthe process of configuring SQL Mail and testing it to make sure the necessary pieces work.

  • 7/31/2019 Configure SQL Mail in SQL Server

    12/12

    Your next step will be to continue testing with various options, and then begin using thissolution 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.

    Fuente : http://www.techrepublic.com/blog/datacenter/how-do-i-configure-sql-mail-in-sql-server-2000/363