How to Send Mail Using SQL Server

Post on 13-Dec-2015

19 views 3 download

description

SQL Server Configure and send email with T-SQL.

Transcript of How to Send Mail Using SQL Server

How To Send Mail Using SQL Server: Part 1

Manish kumar choudhary, 25 Nov 2014 CPOL  10.5K  18

   4.67 (10 votes)

Rate this:

vote 1 vote 2 vote 3 vote 4 vote 5

In this article, we are going to learn the concept of mail sending using SQL Server.

IntroductionThere are three parts for this tutorial:

Configure email profile and send test email using SQL Server Select and send data in mail Schedule daily mail from SQL Server

Step 1

Log in to SQL Server 2008 with a correct user name and password.

Step 2

Expand the Management tab, then select SQL Server Logs, then right-click on Database Mail, then select Configure Database Mail.

Click Next.

Step 4

Select the Set up Database Mail by performing the following tasks: radio button.

Step 6

When you click OK, then write the profile name description and click on Add.

Step 7

Then, a new window appears where you provide your profile name and configure Outgoing Mail Server(SMTP) then click Basic authentication and provide your email id and password then click OK.

Step 8

Then in the new window, it will show your profile, check that and click Next.

Step 9

Then in the new window, the Configure System Parameters description is shown.

Step 10

Click Next. It will show a confirmation about your profile; click Finish.

Step 12

Send a test email by right-clicking on database mail and click Send Test E-Mail.

After the Account and the Profile are created successfully, we need to configure the Database Mail. To configure it, we need to enable the Database Mail XPs parameter using the sp_configure Stored Procedure, as shown here:

Hide   Copy Code

sp_CONFIGURE 'show advanced', 1GORECONFIGUREGOsp_CONFIGURE 'Database Mail XPs', 1GORECONFIGUREGO

Output Message

The Configuration option "show advanced options" changed from 0 to 1. Run the RECONFIGURE statement to install.

The configuration option "Database Mail XPs" changed from 1 to 1. Run the RECONFIGURE statement to install.

Use the following query to send mail using query:

Send Mail Query

USE msdb;--Must Use msdb database, otherwise you will get error.

Hide   Copy Code

GOEXEC sp_send_dbmail @profile_name='MyTestMail',@recipients='manishki@live.com',@subject='My Test Mail Service.',@body='Database Mail Received Successfully.'

This is the body of this message.

Note

If you are using a database other than msdb, then use a stored procedure like msdb.dbo.sp_send_dbmail. The log can be checked in sysmail_log table as shown below:

Hide   Copy Code

SELECT * FROM sysmail_mailitemsGOSELECT * FROM sysmail_logGOselect * from sysmail_log