12 March 2013

database mail configuration


            In this article, I discuss about the Database Mail which is used to send the email using SQL Server. Previously I had discussed about SQL SERVER - Difference Between Database Mail and SQLMail. Database mail is the replacement for SQLMail with many enhancements. So one should stop using SQLMail and upgrade to theDatabase Mail.
In order to send mail using Database Mail in SQL Server, there are 3 basic steps that need to be carried out:
  • Create Profile and Account
  • Configure Email
  • Send Email

Step 1: Create Profile and Account

You need to create a profile and account using the Configure Database Mail Wizard which can be accessed from the Configure Database Mail context menu of the Database Mail node in Management Node.
This wizard is used to manage accounts, profiles, and Database Mail global settings which are shown below:



 
 
 
 


















 







































































































































Step 2: Configure Email

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 through the sp_configure stored procedure, as shown here:
<!--[if !vml]-->http://www.codeproject.com/images/minus.gif<!--[endif]--> Collapse | Copy Code
sp_CONFIGURE 'show advanced', 1 GO RECONFIGURE GO sp_CONFIGURE _
         'Database Mail XPs', 1 GO RECONFIGURE GO 










Step 3: Send Email

After all configurations are done, we are now ready to send an email. To send mail, we need to execute a stored procedure sp_send_dbmail and provide the required parameters as shown below:
<!--[if !vml]-->http://www.codeproject.com/images/minus.gif<!--[endif]--> Collapse | Copy Code
USE msdb GO EXEC sp_send_dbmail @profile_name='PinalProfile', _
@recipients='test@Example.com', @subject='Test message', _
@body='This is the body of the test message. _
Congrats Database Mail Received By you Successfully.' 













After all validations of the parameters entered are done, certain stored procedures are executed and the mail is queued by Service Broker. Read more at SQL SERVER - Introduction to Service Broker.
Database Mail keeps copies of outgoing e-mail messages and displays them in the sysmail_allitems,sysmail_sentitems, sysmail_unsentitems, sysmail_faileditems. The status of the mail sent can be seen insysmail_mailitems table. When the mail is sent successfully, the sent_status field of thesysmail_mailitems table is set to 1 which can again be seen in sysmail_sentitems table. The mails that have failed will have the sent_status field value to 2 and those are unsent will have value 3. The log can be checked insysmail_log table as shown below:
<!--[if !vml]-->http://www.codeproject.com/images/minus.gif<!--[endif]--> Collapse | Copy Code
SELECT * FROM sysmail_mailitems GO SELECT * FROM sysmail_log GO 
















 
 Status can be verified using sysmail_sentitems table.

















After sending mail, you can check the mail received in your inbox, just as I received as shown below:



2 comments:

  1. thank you for giving step-by-step information to configuring the database mail procedure.

    ReplyDelete
    Replies
    1. yes yes i agree whith you man

      Delete