Database Mail - SQL Server

Database Mail is a feature in SQL Server that allows you to send email messages from the database engine. This can be useful for sending alerts and notifications, as well as reports and other information to users.

Here's how to set up and use Database Mail in SQL Server:

  • Enable Database Mail: Before you can use Database Mail, you need to enable it in SQL Server. This can be done using SQL Server Management Studio or by running a T-SQL script. To enable Database Mail, go to the "Management" node in SSMS, right-click on "Database Mail" and select "Configure Database Mail".
  • Configure SMTP Server: To send email messages, you need to configure an SMTP server. This can be done in the "Configure Database Mail" wizard or by running a T-SQL script. You'll need to enter the server name, port number, and login credentials for the SMTP server.
  • Create a Profile: In SQL Server, a profile is a collection of settings that define how email messages should be sent. To create a new profile, go to the "Management" node in SSMS, right-click on "Database Mail" and select "New Profile". Enter a name for the profile and select the SMTP server you configured earlier.
  • Configure Profile Properties: Once you've created a profile, you'll need to configure its properties. This includes the email address that messages should be sent from, the reply-to address, and other settings such as whether to include attachments and how to handle errors.
  • Send Test Message: To test Database Mail, you can use the "Test Email" feature in SSMS. This allows you to send a test message and verify that it was delivered successfully.

Here's an example of how to send an email message using Database Mail in SQL Server:

EXEC msdb.dbo.sp_send_dbmail  

    @profile_name = 'MyProfile',  

    @recipients = 'Harsh@example.com',  

    @subject = 'Test Email',  

    @body = 'This is a test email sent from SQL Server.';  

This T-SQL script sends an email message using the "MyProfile" profile and sends it to the email address "Harsh@example.com". The subject of the email is "Test Email" and the body contains the text "This is a test email sent from SQL Server."

Database Mail is a powerful feature in SQL Server that allows you to send email messages directly from the database engine. By setting up and configuring Database Mail, you can automate the process of sending alerts, notifications, and reports to users, making it easier to manage your SQL Server environment.

Comments

Popular posts from this blog

COPILOT Feature in SQL Server 2025

Prefetching - SQL Server

Split Brain - SQL Server