Here is a good example on sqltips -
http://www.mssqltips.com/tip.asp?tip=1100 And here is a scripted approach.
-- Create a Database Mail profile
EXECUTE msdb.dbo.sysmail_add_profi
le_sp
@profile_name = 'SQL2005_Email_ProfileName
',
@description = 'Notification service for SQL Server' ;
-- Create a Database Mail account
EXECUTE msdb.dbo.sysmail_add_accou
nt_sp
@account_name = 'SQL Server Notification Service',
@description = 'SQL Server Notification Service',
@email_address = 'john.doe@domain_name.com'
,
@replyto_address = 'john.doe@domain_name.com'
,
@display_name = 'SQL Server Notification Service',
@mailserver_name = 'smtpserver.domain_name.co
m' ;
-- Add the account to the profile
EXECUTE msdb.dbo.sysmail_add_profi
leaccount_
sp
@profile_name = 'SQL2005_Email_ProfileName
',
@account_name = 'SQL Server Notification Service',
@sequence_number =1 ;
-- Grant access to the profile to the DBMailUsers role
EXECUTE msdb.dbo.sysmail_add_princ
ipalprofil
e_sp
@profile_name = 'SQL2005_Email_ProfileName
',
@principal_id = 0,
@is_default = 1 ;
SELECT * FROM msdb.dbo.sysmail_profile
SELECT * FROM msdb.dbo.sysmail_account
to test:
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_fi
le = ] attach_query_result_as_fil
e ]
[ , [ @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 ]
[ , [ @query_result_no_padding = ] query_result_no_padding ]
[ , [ @mailitem_id = ] mailitem_id ] [ OUTPUT ]
Cheers,
Hades666