使用sql2008 高效发送邮件

Time:2019/4/21  keywords:使用sql2008 高效发送邮件

实现此要求需要分两步进行

第一步sql2008部分配置:

如下图所示,在SQL Server Management Studio中启动配置数据库邮件。

 

 

 

 

 

 

 

 

 


 

这时候,SQL 方式发送测试邮件则是如下的SQL 语句

exec msdb..sp_send_dbmail
 @profile_name =  'ANDY的邮件配置'           -- profile 名称
 ,@recipients   =  'abx@163.com'          -- 收件人邮箱
 ,@subject      =  'SQL Server 2005 Mail Test' -- 邮件标题
 ,@body         =  'Hello Mail!'               -- 邮件内容
 ,@body_format  =  'TEXT'                      -- 邮件格式

 

参考资料: 

在 SQL Server 2005/2008 中,使用 Gmail 帐号配置数据库邮件功能 
http://space.zdnet.com.cn/html/82/289382-2777798.html


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 ]


参数

  • @profile_name = ] 'profile_name'

  • 发送邮件的配置文件的名称。profile_name 的类型为 sysname,默认值为 NULL。profile_name 必须是现有数据库邮件配置文件的名称。如果未指定profile_name,则 sp_send_dbmail 使用当前用户的默认专用配置文件。如果该用户没有默认专用配置文件,sp_send_dbmail 会使用msdb 数据库的默认公共配置文件。如果用户没有默认的专用配置文件,而且数据库也没有默认的公共配置文件,则必须指定 @profile_name

  • @recipients = ] 'recipients'

  • 要向其发送邮件的电子邮件地址列表,以分号分隔。收件人列表的类型为 varchar(max)。虽然此参数是可选参数,但是必须至少指定@recipients@copy_recipients 或 @blind_copy_recipients 中的一个,否则sp_send_dbmail 将返回错误。

  • @copy_recipients = ] 'copy_recipients'

  • 要向其抄送邮件的电子邮件地址列表,以分号分隔。抄送件收件人列表的类型为 varchar(max)。虽然此参数是可选参数,但是必须至少指定@recipients@copy_recipients 或 @blind_copy_recipients 中的一个,否则sp_send_dbmail 将返回错误。

  • @blind_copy_recipients = ] 'blind_copy_recipients'

  • 要向其密件抄送邮件的电子邮件地址列表,以分号分隔。密件副本收件人列表的类型为 varchar(max)。虽然此参数是可选参数,但是必须至少指定@recipients@copy_recipients 或 @blind_copy_recipients 中的一个,否则sp_send_dbmail 将返回错误。

  • @subject = ] 'subject'

  • 电子邮件的主题。主题的类型为 nvarchar(255)。如果未指定主题,则默认为“SQL Server 消息”。

  • @body = ] 'body'

  • 电子邮件的正文。邮件正文的类型为 nvarchar(max),默认值为 NULL。

  • @body_format = ] 'body_format'

  • 邮件正文的格式。该参数的类型为 varchar(20),默认值为  NULL。如果已指定,则待发邮件的标头设置会指示邮件正文具有指定格式。该参数可能包含下列值之一:

    默认为 TEXT。

    • TEXT

    • HTML

  • @importance = ] 'importance'

  • 邮件的重要性。该参数的类型为 varchar(6)。该参数可能包含下列值之一:

    默认值为 Normal。

    • Low

    • Normal

    • High

  • @sensitivity = ] 'sensitivity'

  • 邮件的敏感度。该参数的类型为 varchar(12)。该参数可能包含下列值之一:

    默认值为 Normal。

    • Normal

    • Personal

    • Private

    • Confidential

  • @file_attachments = ] 'file_attachments',

  • 电子邮件附件的文件名列表,以分号分隔。必须使用绝对路径指定列表中的文件。附件列表的类型为nvarchar(max)

  • @query = ] 'query'

  • 要执行的查询。查询结果可以作为文件附加,或包含在电子邮件的正文中。查询的类型为 nvarchar(max),并且可以包含任何有效的 Transact-SQL 语句。请注意,查询在单独的会话中执行,所以调用sp_send_dbmail 的脚本中的局部变量不可用于查询。

  • @execute_query_database = ] 'execute_query_database'

  • 存储过程在其中运行查询的数据库上下文。该参数的类型为 sysname,默认为当前数据库。只有在指定@query 时,此参数才适用。

  • @attach_query_result_as_file = ] attach_query_result_as_file

  • 指定查询结果集是否作为附件返回。attach_query_result_as_file 的数据类型为 bit,默认值为 0。

    如果该值为 0,则查询结果包含在电子邮件的正文中,在 @body 参数的内容之后。如果该值为 1,则结果作为附件返回。只有在指定@query 时,此参数才适用。

  • @query_attachment_filename = ] query_attachment_filename

  • 指定查询结果集附件使用的文件名。query_attachment_filename 的数据类型为 nvarchar(255),默认值为 NULL。如果attach_query_result 为 0,则忽略此参数。如果 attach_query_result 为 1 且此参数为 NULL,则数据库邮件会创建任意文件名。

  • @query_result_header = ] query_result_header

  • 指定查询结果是否包含列标题。query_result_header 值的数据类型为 bit。如果该值为 1,则查询结果包含列标题。如果该值为 0,则查询结果不包含列标题。该参数的默认值为1。只有在指定 @query 时,此参数才适用。

  • @query_result_width = ] query_result_width

  • 用于设置查询结果的格式的线条宽度(字符)。query_result_width 的数据类型为 int,默认值为 256。提供的值必须介于 10 和 32767 之间。只有在指定@query 时,此参数才适用。

  • @query_result_separator = ] 'query_result_separator'

  • 用于分隔查询输出中的列的字符。分隔符的类型为 char(1)。默认为“ ”(空格)。

  • @exclude_query_output = ] exclude_query_output

  • 指定是否使用电子邮件返回查询执行的输出。exclude_query_output 的数据类型为 bit,默认值为 0。当此参数为 0 时,sp_send_dbmail 存储过程的执行将在控制台上打印作为查询执行结果而返回的消息。当此参数为 1 时,sp_send_dbmail 存储过程的执行不会在控制台上打印任何查询执行消息。

  • @append_query_error = ] append_query_error

  • 指定是否在 @query 参数指定的查询返回错误时发送电子邮件。append_query_error 的数据类型为bit,默认值为 0。如果此参数的值为 1,则数据库邮件会发送电子邮件,并电子邮件的正文中显示查询错误消息。如果此参数的值为 0,则数据库邮件不发送电子邮件,sp_send_dbmail 在结束时会返回代码 1,指示失败。

  • @query_no_truncate = ] query_no_truncate

  • 指定是否使用可避免截断大型可变长度数据类型(varchar(max)nvarchar(max)varbinary(max)xmltextntextimage 和用户定义类型)的选项执行查询。设置该选项后,查询结果将不包含列标题。query_no_truncate 值的数据类型为 bit。当该值为 0 或未指定时,查询中的列将截断为 256 个字符。当该值为 1 时,不截断查询中的列。此参数的默认值为 0。

    注意:与大量数据一起使用时,@query_no_truncate 选项会占用其他资源,并可降低服务器的性能。

  • @mailitem_id = ] mailitem_id [ OUTPUT ]

  • 可选输出参数将返回消息的 mailitem_idmailitem_id 的类型为 int

返回代码值

0(成功)或 1(失败)

结果集

成功时,返回消息“邮件已排队”。