Adventures in Machine Learning

Streamline Your Email Communication with SQL Server Database Mail

In today’s world, it is essential to have reliable and secure methods of communicating information. Companies often require a system that permits the sending of emails directly from their applications to a chosen email address.

The SQL Server Database Mail provides a feature-rich and scalable solution for many industries, and in this article, we will discuss how to configure and use the Database Mail with ease.

Definition and Functionality

The SQL Server Database Mail is a system that enables users to send email messages directly from SQL Server databases. Unlike traditional email applications, it can send emails in both plain text and HTML formats with attached files.

This interface is built to be reliable, scalable, and secure, providing support for both synchronous and asynchronous emailing.

Reliability and Scalability of Database Mail

The Database Mail is programmed to use the SMTP protocol for message transport. The system holds an independent email queue service which operates in a separate process, making it less prone to system usage bottlenecks.

This design also means that should an email send failure occur, the system will have a sequence of connectable and operable SMTP servers to try next irregardless of the applied SMTP server. The sp_send_dbmail stored procedure is a useful command that schedules and disseminates emails via a queue process.

It allows you to specify various criteria, such as a recipient list, subject, body text, and attachments. Since the sending procedure is asynchronous, it executes quickly and efficiently.

Security and Supportability of Database Mail

The Database Mail feature is typically disabled by default, and users must enable it from the configuration settings. Once enabled, users can manage its usage through appraising various command-line options and interface configurations.

Before users start making use of the system, they should ensure that they have been assigned the DatabaseMailUserRole role. The system incorporates Secure Mail Profiles, which ensure that emails are secure, confidential and can only be sent through authenticate accounts.

This method is achieved by authenticating the Database Mail credentials or Proxy accounts. It is also possible to limit the size of attached files, audit and log mail messages.

Configuring SQL Server Database Mail

Show Advanced Configuration Settings

The first step in configuring the SQL Server Database Mail is executing sp_configure in SQL Server Management Studio. By running this specialty stored procedure, a user can inspect and edit prevalent configuration options at the global level.

Enable Database Mail

To allow the Database Mail, execute the following command in SQL Server Management Studio. sp_configure ‘Database Mail XPs’,1, followed by the reconfiguration statement, which transforms these configuration settings from the non-functional state to functional working state.

Create a Database Mail Account

The primary feature of creating an account is to identify the SMTP server through which the email messages were to be processed. To create a Database Mail account, use the msdb.dbo.sysmail_add_account_sp stored procedure.

Create a Database Mail Profile

Profiles present the level for grouping Database Mail accounts. A Database Mail profile signifies one or various Database Mail accounts settings, and name it for easy identification.

New profiles are created using the msdb.dbo.sysmail_add_profile_sp system stored procedure.

Add Account to Public Profile

Once you have created a new account, you must add it to a public profile to enable the account to be utilized. The msdb.dbo.sysmail_add_profileaccount_sp stored procedure is used to add the account.

Grant Access to Profile

To grant access to your created profile, use the appropriate system to store procedures. By default, all users in the msdb database are given access to the Database Mail feature; therefore, the role of the mailing profile may be administratively appointed to each valid instance’s database.


The SQL Server Database Mail is an indispensable tool that every company must take advantage of. This rich-featured and scalable application provides a reliable and secure way of communicating information, whether in plain text, HTML, or with attachments.

Understanding how to configure and use the Database Mail can save businesses money, time, and prevent data piracy. We hope this guide has been helpful, and please feel free to explore Database Mail functionality further to discover its benefits.The SQL Server Database Mail function gives businesses a scalable and secure way of communicating information via email directly from their applications.

The system sends emails in plain text or HTML formats, with attached files. In this article, we will discuss how to send various email messages using Database Mail, including those containing query results.

Send Email Message Example

One of the most common ways of using the Database Mail function is through the ‘sp_send_dbmail’ stored procedure. This stored procedure allows you to specify the recipient’s email address, subject, body text, and attachment details.

Here is an example of how to send a plain text email using ‘sp_send_dbmail’:

EXEC msdb.dbo.sp_send_dbmail

@profile_name = ‘MAIL_PROFILE’,

@recipients = ‘[email protected]’,

@subject = ‘Email Subject’,

@body = ‘Email Body Text’

By running this procedure on your SQL Server Management Studio server, the system will create a new email message and send it directly to the recipient’s email address provided. The email message will contain the information specified in the parameters you used to create it.

Send Email Message with Query Result Example

The process for sending emails containing query results is different from plain text emails. The first step in this process is to create a query that retrieves the information you want to send via email.

Here is an example:

SELECT * FROM Inventory

Once you have created the query, you will need to convert the query results into a format that can be sent in an email. For this, we will convert the query result to an HTML table.

Here is an example:




SELECT td = InventoryID, ”, td = ProductName, ”, td = QuantityPerUnit, ”, td = UnitsInStock, ”

FROM Inventory





Once you have created the HTML table, you can use the ‘sp_send_dbmail’ stored procedure to send it as an email. Here is an example:

EXEC msdb.dbo.sp_send_dbmail

@profile_name = ‘MAIL_PROFILE’,

@recipients = ‘[email protected]’,

@subject = ‘Inventory Report’,

@body = ‘Inventory Report is attached.’,

@body_format = ‘HTML’,

@query = ‘SELECT * FROM Inventory’,

@attach_query_result_as_file = 1,

@query_attachment_filename = ‘Inventory.csv’,

@query_result_separator = ‘,’,

@query_result_no_padding = 1,

@query_result_header = 1,

@query_result_width = 32767,

@query_result_style = ‘FONT-FAMILY: Arial; FONT-SIZE: 9pt;’

@file_attachment = ‘C:Inventory.csv’

This stored procedure will send an email message containing the query result as an HTML table and attached CSV file.


The SQL Server Database Mail feature provides a secure and scalable solution for businesses to communicate information via email from their applications. With ‘sp_send_dbmail’, you can easily send plain text and HTML emails with attachments.

To send emails that contain query results, you can convert the results to an HTML table and attach a file containing the data. We hope this article helps you understand how to send various types of emails using Database Mail.

In conclusion, SQL Server Database Mail is a reliable, scalable, and secure system for businesses to send email messages from their applications. By using the ‘sp_send_dbmail’ stored procedure, users can send plain text and HTML email messages with attachments.

To send emails containing query results, they can convert the results into an HTML table and attach a file. Proper configuration of the Database Mail feature is important to ensure its functionality and security.

Understanding how to use and configure Database Mail is essential for businesses today to communicate their information effectively.

Popular Posts