Use the Database Mail XPs option to enable Database Mail on this server. The possible values are:
- 0 indicating Database Mail is not available (default).
- 1 indicating Database Mail is available.
The setting takes effect immediately without a server stop and restart.
After enabling Database Mail, you must configure a Database Mail host database to use Database Mail.
Configuring Database Mail using the Database Mail Configuration Wizard enables the Database Mail extended stored procedures in the msdb database. If you use the Database Mail Configuration Wizard, you do not have to use the sp_configure example below.
Setting the Database Mail XPs option to 0 prevents Database Mail from starting. If it is running when the option is set to 0, it continues to run and send mail until it is idle for the time configured in the DatabaseMailExeMinimumLifeTime option.
The following example enables the Database Mail extended stored procedures.
sp_configure ‘show advanced options’, 1;
GO
RECONFIGURE;
GO
sp_configure ‘Database Mail XPs’, 1;
GO
RECONFIGURE
GO
EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = ‘notifications’,
@description = ‘profile for sending outgoing notifications’
GO
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
@profile_name = ‘notifications’,
@principal_name = ‘public’,
@is_default = 1;
GO
EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = ‘Gmail’,
@description = ‘Account for outgoing notifications’,
@email_address = ‘mrnettek@gmail.com’,
@display_name = ‘SQL Trigger Alert’,
@mailserver_name = ‘smtp.gmail.com’,
@port = 587,
@enable_ssl = 1,
@username = ‘mrnettektesting@gmail.com’,
@password = ‘NotReallyMyPassword’
GO
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = ‘Notifications’,
@account_name = ‘Gmail’,
@sequence_number = 1
GO
Code to use to send email
EXEC msdb.dbo.sp_send_dbmail
@profile_name = ‘notifications’,
@recipients = ‘mrnettektesting@gmail.com’,
@body = ‘This is a test’, —- could also be contents of a change
@subject = ‘Change is product notification’
GO
Sample email
Further Information
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.
Database Mail is designed for reliability, scalability, security, and supportability.
Reliability
- Database Mail uses the standard Simple Mail Transfer Protocol (SMTP) to send mail. You can use Database Mail without installing an Extended MAPI client on the computer that runs SQL Server.
- Process isolation. To minimize the impact on SQL Server, the component that delivers e-mail runs outside of SQL Server, in a separate process. SQL Server will continue to queue e-mail messages even if the external process stops or fails. The queued messages will be sent once the outside process or SMTP server comes online.
- Failover accounts. A Database Mail profile allows you to specify more than one SMTP server. Should an SMTP server be unavailable, mail can still be delivered to another SMTP server.
- Cluster support. Database Mail is cluster-aware and is fully supported on a cluster.
Scalability
- Background Delivery: Database Mail provides background, or asynchronous, delivery. When you call sp_send_dbmail to send a message, Database Mail adds a request to a Service Broker queue. The stored procedure returns immediately. The external e-mail component receives the request and delivers the e-mail.
- Multiple profiles: Database Mail allows you to create multiple profiles within a SQL Server instance. Optionally, you can choose the profile that Database Mail uses when you send a message.
- Multiple accounts: Each profile can contain multiple failover accounts. You can configure different profiles with different accounts to distribute e-mail across multiple e-mail servers.
- 64-bit compatibility: Database Mail is fully supported on 64-bit installations of SQL Server.
Security
- Off by default: To reduce the surface area of SQL Server, Database Mail stored procedures are disabled by default.
- Mail Security:To send Database Mail, you must be a member of the DatabaseMailUserRole database role in the msdb database.
- Profile security: Database Mail enforces security for mail profiles. You choose the msdb database users or groups that have access to a Database Mail profile. You can grant access to either specific users, or all users in msdb. A private profile restricts access to a specified list of users. A public profile is available to all users in a database.
- Attachment size governor: Database Mail enforces a configurable limit on the attachment file size. You can change this limit by using the sysmail_configure_sp stored procedure.
- Prohibited file extensions: Database Mail maintains a list of prohibited file extensions. Users cannot attach files with an extension that appears in the list. You can change this list by using sysmail_configure_sp.
- Database Mail runs under the SQL Server Engine service account. To attach a file from a folder to an email, the SQL Server engine account should have permissions to access the folder with the file.
Supportability
- Integrated configuration: Database Mail maintains the information for e-mail accounts within SQL Server Database Engine. There is no need to manage a mail profile in an external client application. Database Mail Configuration Wizard provides a convenient interface for configuring Database Mail. You can also create and maintain Database Mail configurations using Transact-SQL.
- Logging. Database Mail logs e-mail activity to SQL Server, the Microsoft Windows application event log, and to tables in the msdb database.
- Auditing: Database Mail keeps copies of messages and attachments sent in the msdb database. You can easily audit Database Mail usage and review the retained messages.
- Support for HTML: Database Mail allows you to send e-mail formatted as HTML.
Database Mail consists of the following main components:
- Configuration and security componentsDatabase Mail stores configuration and security information in the msdb database. Configuration and security objects create profiles and accounts used by Database Mail.
- Messaging componentsThe msdb database acts as the mail-host database that holds the messaging objects that Database Mail uses to send e-mail. These objects include the sp_send_dbmail stored procedure and the data structures that hold information about messages.
- Database Mail executableThe Database Mail executable is an external program that reads from a queue in the msdb database and sends messages to e-mail servers.
- Logging and auditing componentsDatabase Mail records logging information in the msdb database and the Microsoft Windows application event log.
Configuring Agent to use Database Mail:
SQL Server Agent can be configured to use Database Mail. This is required for alert notifications and automatic notification when a job completes.
Caution |
---|
Individual job steps within a job can also send e-mail without configuring SQL Server Agent to use Database Mail. For example, a Transact-SQL job step can use Database Mail to send the results of a query to a list of recipients. |
You can configure SQL Server Agent to send e-mail messages to predefined operators when:
- An alert is triggered. Alerts can be configured to send e-mail notification of specific events that occur. For example, alerts can be configured to notify an operator of a particular database event or operating system condition that may need immediate action. For more information about configuring alerts.
- A scheduled task, such as a database backup or replication event, succeeds or fails. For example, you can use SQL Server Agent Mail to notify operators if an error occurs during processing at the end of a month.