Configure Database Mail In SQL Server To Send E-Mail From SQL Server Database

What is Database Mail

Database Mail is an enterprise solution for sending e-mail messages from the SQL Server Database Engine. Using Database Mail, your database applications can send e-mail messages to users. The messages can contain query results, and can also include files from any resource on your network.

Step 1: 

Firstly, we need Microsoft SQL Server Management Studio (SSMS) installed on our system (PC or Laptop) to send mail from SQL Server. So if you do not installed SSMS on your system, then first download and install it.
 
Step 2:
  1. Now Open SSMS and connect it to your SQL Server instance.

  2. Expand Management option, there you can find Database Mail option. As you can see in the following image.



  3. Now Right click on Database Mail, and select Configure Database Mail option.

  4. This will open a Database Mail Configuration wizard. Select next to continue.

  5. Now choose first option, Set up Database Mail by performing the following tasks, and click next.



  6. Now give your database mail Profile Name and its description as whatever you want. Here I am giving "Test Profile" as profile name. In SMTP account section, Select Add account option to create new SMTP account as in the following image.



  7. Now in New Database Mail Account window, fill up the details as in the following image. Here I am using gmail account to send email. So I used smtp.gmail.com as Server name and 587 as Port number.

    In SMTP Authentication and Outgoing Mail Server, use your email address and password from which you want to send e-mail. Also make sure that " The server requires a secure connection" check box must be checked for the security purpose. Now click OK when you are done and click next to go to the next screen.

Step 3:

The next screen
is about to make Database mail profile either public or private. You can leave this option and move to next screen and again select next to finish the creation process of Database Mail profile.

Now you are all set up to send e-mail. Now go to Management and right click on Database Mail and select Send Test E-mail... option to send test mail as in the following image.



Choose Test Profile as Database Mail Profile from D
ropdown and enter To email address, Subject and Body, and click Send Test E-Mail. Now your e-mail has been sent successfully from SQL Server.


Step 4:

If you want to send mail using SQL query then you can use system database 'msdb' and its stored procedure 'sp_send_dbmail' as follows:

  1. EXEC msdb.dbo.sp_send_dbmail  
  2.    @profile_name = 'Test Profile'  
  3.    @recipients = '[email protected]',  
  4.    @subject = 'Test Mail',  
  5.    @body = 'This is the test mail.',  
  6.    @body_format = 'HTML' 

To check status of your sent e-mail, you can execute the following query in SQL Server:

  1. USE msdb  
  2. GO  
  3. SELECT *  
  4. FROM sysmail_allitems 

In this query result, you can find all the details about sent mail from SQL Server. You can check the sent_status column to check the status of mail.

So, this is all about Database Mail in SQL Server to send e-mail. I hope you understood my explanation and liked this article.

Thank you!

Up Next
    Ebook Download
    View all
    Learn
    View all