vendredi, janvier 27, 2006

SQL 2005 Database mail

Database Mail is an enterprise solution for sending e-mail messages from the Microsoft SQL Server 2005 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..

How to enable Database mail support
Go to Programs > Microsoft SQL Server 2005 >Configuration Tools > SQL Server Surface Area Configuration, click then on Surface Area Configuration for features, you will obtain this windows: on the DB mail configuration, check the enable option, note that your Database compatibility level must be set to 90 (SQL Server 2005).

Send a mail using Database Mail
First you must configuration a profile, in the sql server administration console, go to management, right click on email -> config, the wizard helps you to create it.

Database Mail is an enterprise solution for sending e-mail messages from the Microsoft SQL Server 2005 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..

How to enable Database mail support
Go to Programs > Microsoft SQL Server 2005 >Configuration Tools > SQL Server Surface Area Configuration, click then on Surface Area Configuration for features, you will obtain this windows: on the DB mail configuration, check the enable option, note that your Database compatibility level must be set to 90 (SQL Server 2005).

Send a mail using Database Mail
First you must configuration a profile, in the sql server administration console, go to management, right click on email -> config, the wizard will helps you to create it.

to test the profile you can user "test e-mail send" on the context menu:

then use this Stored procedure to send e-mails from your application, Sps, triggers ...:
sp_send_dbmail [ [ @profile_name = ] 'profile_name' ]
[ , [ @recipients = ] 'recipients [ ; ...n ]' ]
[ , [ @copy_recipients = ] 'copy_recipient [ ; ...n ]' ]
[ , [ @blind_copy_recipients = ] 'blind_copy_recipient [ ; ...n ]' ]
[ , [ @subject = ] 'subject' ]
[ , [ @body = ] 'body' ]
[ , [ @body_format = ] 'body_format' ]
[ , [ @importance = ] 'importance' ]
[ , [ @sensitivity = ] 'sensitivity' ]
[ , [ @file_attachments = ] 'attachment [ ; ...n ]' ]
[ , [ @query = ] 'query' ]
[ , [ @execute_query_database = ] 'execute_query_database' ]
[ , [ @attach_query_result_as_file = ] attach_query_result_as_file ]
[ , [ @query_attachment_filename = ] query_attachment_filename ]
[ , [ @query_result_header = ] query_result_header ]
[ , [ @query_result_width = ] query_result_width ]
[ , [ @query_result_separator = ] 'query_result_separator' ]
[ , [ @exclude_query_output = ] exclude_query_output ]
[ , [ @append_query_error = ] append_query_error ]
[ , [ @query_no_truncate = ] query_no_truncate ]
[ , [ @mailitem_id = ] mailitem_id ] [ OUTPUT ]

For example :
EXEC msdb.dbo.sp_send_dbmail
@recipients=N'toto@microsoft.co',
@body=N'Hello Mr toto from Microsoft ...' ;

Aucun commentaire: