Presentation on theme: "Database Mail. What is Database Mail? In a short answer, Database Mail is a feature of SQL Server which let Database sends Mail or SMS to anybody after."— Presentation transcript:
What is Database Mail? In a short answer, Database Mail is a feature of SQL Server which let Database sends Mail or SMS to anybody after a transaction
SQL Server has a functionality to do such a thing. In fact whenever a transaction commits or Rollbacks after that SQL Server can send SMS or to anybody with any limitation.
Database Mail run outside of SQL Server so it is not pressure on your SQL Server Engine. It also is support clustered environment and can use SMTP Servers. On the other hand it sends mails asynchronously with Service broker so there will be no waste time and it has some security maintenance which let you filter messages
The best example to send SMS is after a Backup procedure it doesn’t matter that it is commit or rollback, you will receive SMS (mail) after the procedure and you can remotely run the backup procedure after a rollback. It is very good for DBAs
When SQL Server 2005 was released, the added functionality of Database Mail came with it, giving us the ability to use SMTP to send from SQL Server instances. This was a huge leap forward from SQL Server 2000 SQL Mail, in that you no longer needed to set-up a mail client first (such as Outlook) to send s.
Reliability No Microsoft Outlook or Extended MAPI requirement. Database Mail uses SMTP to send mail. Process isolation. To minimize the impact on SQL Server, the component that delivers runs outside of SQL Server, in a separate process. SQL Server will continue to queue 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 component receives the request and delivers the . 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 across multiple 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. To send Database Mail, you must be a member of the Databas UserRole 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.
Security 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. sysmail_configure_sp 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 , the SQL Server engine account should have permissions to access the folder with the file.
Supportability Integrated configuration. Database Mail maintains the information for 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.
With above information you can guess that what the Database Mail requirements are. SQL Server (2005 – 2008) – SQL Express don’t have Database Mail. Enabling Service Broker Enabling Database Mail A Mail Server (Usually Exchange Server) to send Mails. Configuring Database Mail to use it.
How to Configure Database Mail Database Mail is disabled by default and you have to enable it. To enabling it you have three options. First one is to use Database Mail Configuration Wizard. The Second solution to configure Database Mail, is to use Store procedures. The third solution is using the Surface Area Configuration facet of Policy-Based Management.
Configure Database Mail SQL Server 2008
Enabling and configuring Database Mail in SQL Server using T-SQL The second method is to use Transact SQL (T-SQL) and stored procedures to enable and configure Database Mail, which is much quicker, and less prone to human error, after the initial run has been tested and confirmed as working. databas -in-sql-server-using-t-sql/ databas -in-sql-server-using-t-sql/
Enabling and configuring Database Mail in SQL Server using T-SQL This walk through will execute a number of stored procedures to accomplish the following tasks: Enable the Database Mail feature Create a profile for Database Mail Create an account for use with the profile Send a test to the DBAs address Enable the SQL Server Agent to use Database Mail Add the ‘DBAs’ as an operator for notifications Before running any of scripts below, I would take a backup of your system databases (master, msdb), and ensure you have sysadmin rights.
Enable the Database Mail XPs: USE master GO sp_configure 'show advanced options',1 GO RECONFIGURE WITH OVERRIDE GO sp_configure 'Database Mail XPs',1 GO RECONFIGURE GO
Create a new mail profile: USE msdb GO EXECUTE msdb.dbo.sysmail_add_profile_sp = 'admin', = 'Profile for sending Automated DBA Notificati ons' GO
Create an account for the notifications (changing the address, mail server, port as appropriate to your environment): EXECUTE msdb.dbo.sysmail_add_account_sp = 'SQLAlerts', = 'Account for Automated DBA Notifications', = = 'SQL Alerts', = 'smtp.example.com', = 25 GO
Add the account to the profile: EXECUTE msdb.dbo.sysmail_add_profileaccount_sp = 'admin', = 'SQLAlerts', = 1 GO
Enable the SQL Server Agent to use Database Mail profile by updating the registry settings: USE msdb GO EXEC master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'UseDatabas ', N'REG_DWORD', 1 EXEC master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'Databas Profile', N'REG_SZ', N'admin‘
NOTE: (0 row(s) affected) is normal here
Restart the SQL Server Agent: At this point, the SQL Server Agent needs to be restarted. If it isn’t, the Database Mail configuration changes will not be picked up, and the Database Mail process will not start / function correctly. If Database Mail is being configured on a SQL Server cluster, you’ll need to perform this using the Cluster Administrator tool by selecting the appropriate cluster group, then restarting the SQL Server Agent resource for the appropriate instance: Windows Server 2003: C:\WINDOWS\Cluster\CluAdmin.exe Windows Server 2008: C:\Windows\System32\Cluadmin.msc
Once the SQL Server Agent has been restarted, try sending an to test the configuration is working as expected: EXECUTE msdb.dbo.sp_send_dbmail = 'admin', = = 'Test Message generated from SQL Server Database Mail', = 'This is a test message from SQL Server Database Mai l' GO
Finally, setup an operator called ‘DBAs’ for the job notifications (24×7 schedule in this case) for the address you supplied earlier: EXEC GO
To generate notifications when a job succeeds, fails, or completes, you can run a stored procedures like below on a job by job basis: USE msdb GO EXEC databases - ba ckups.Subplan_1', GO NOTE: The notify_levels can be set to: 1 (job succeeds), 2 (job fails), or 3 (job completes)
To enable failure notifications for all jobs, run the following script, which will update the notifications jobs for you, and output the T-SQL that’s been executed for each job identified: vel NVARCHAR(3000) DECLARE job_operator_cursor CURSOR FOR SELECT name, job_id, notify_level_ FROM msdb.dbo.sysjob s_view OPEN job_operator_cursor FETCH NEXT FROM @ NotifyLevel WHILE = 0 BEGIN IF NOT EXISTS(SELECT 1 FROM msdb.dbo.sysjobs_view WHERE noti fy_level_ = 2 and name
BEGIN PRINT '' = 'EXEC msdb.dbo.sp_update_job obName+''', EXEC END FETCH NEXT FROM @ NotifyLevel END
CLOSE job_operator_cursor DEALLOCATE job_operator_cursor
Quick troubleshooting queries for Database Mail Check to see if the service broker is enabled (should be 1): SELECT is_broker_enabled FROM sys.databases WHERE name = 'msdb' Check to see if Database Mail is started in the msdb database: EXECUTE dbo.sysmail_help_status_sp …and start Database Mail if necessary: EXECUTE dbo.sysmail_start_sp
Quick troubleshooting queries for Database Mail Check the status of the mail queue: = 'Mail' Check the Database Mail event logs: SELECT * FROM sysmail_event_log Check the mail queue for the status of all items (including sent mails): SELECT * FROM sysmail_allitems
Logging. Database Mail logs 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 formatted as HTML.