调用上面这个SP来发邮件:
  EXEC sp_SendDatabaseMail
  @recipients = '001@test.com; 002@test.com;'
  @body = 'This is a testing mail',
  @HtmlBody = 'Testing Database Mail'
  SQL Server 2005起,使用Database Mail,脚本如下:
  --1. 启用database mail
  use master
  GO
  exec sp_configure 'show advanced options',1
  reconfigure
  exec sp_configure 'Database mail XPs',1
  reconfigure
  GO
  --2. 添加account
  exec msdb..sysmail_add_account_sp
  @account_name            = 'SqlAlert'                -- mail account
  ,@email_address           = 'SqlAlert@test.com'       -- sendmail address
  ,@display_name            = 'SqlAlert'                -- sendusername
  ,@replyto_address         = null
  ,@description             = null
  ,@mailserver_name         = '***,***,***,***'         -- SMTP Address
  ,@mailserver_type         = 'SMTP'                    -- SQL 2005 only support SMTP
  ,@port                    = 25                        -- port
  --,@username                = '*********@test.com'    -- account
  --,@password                = '******************'    -- pwd
  ,@use_default_credentials = 0
  ,@enable_ssl              = 0                         --is ssl enabled on SMTP server
  ,@account_id              = null
  --3. 添加profile
  exec msdb..sysmail_add_profile_sp
  @profile_name = 'SqlAlert'         -- profile name
  ,@description  = 'dba mail profile' -- profile description
  ,@profile_id   = null
  --4. 关联account and profile
  exec msdb..sysmail_add_profileaccount_sp
  @profile_name    = 'SqlAlert'     -- profile name
  ,@account_name    = 'SqlAlert'     -- account name
  ,@sequence_number = 1              -- account order in profile
  --5. 发送database mail
  EXEC msdb.dbo.sp_send_dbmail
  @profile_name = 'SqlAlert',
  @recipients = '001@test.com; 002@test.com;'
  @body = 'This is a testing mail',
  @subject = 'Testing Database Mail';
  GO
  注意:SMTP服务器的配置,比如:是否使用smtp用户验证,SSL是否开启,必须要和服务端一致,否则无法发送邮件。
  其他
  (1) 告警的次数:被告警的问题也许正在处理中,告警还在反复频繁发送,尤其用脚本轮询时,注意设置次数和发送间隔;
  (2) 告警的历史记录:短信或者邮件告警,好都在数据库中留一份记录;
  (3) 字符编码:如果应用程序/接口不支持中文,可以把中文转成UTF-8的字符编码发送,然后再解析回来。