这篇文章主要介绍SQL数据库msdb.dbo.sp_send_dbmail函数发送邮件的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
广州网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。创新互联从2013年成立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
在推行系统中,时不时会有用户提出希望系统能自动推送邮件,由于手头的工具和能力有限,不少需求都借助于sql server的邮件触发来实现。
步骤:
1、配置邮箱。步骤略,网上有不少帖子说明,手工直接在管理-数据库邮件配置即可。配置完成后可以右键测试邮箱是否正常工作。
2、制作发送邮件脚本
3、sql server 代理定义周期计划
邮件脚本编写:
场景一:业务部门希望可以每周提供一次样品库存,即将sql查询的结果以附件的方式发给指定的人员。
EXEC msdb.dbo.sp_send_dbmail
@profile_name = '<账户名>', --定义好的sql server 邮箱账户名
@recipients = '', --需要发送邮件的账号,多个用;间隔,建议通过一个邮件组来管理需要发送的地址
@body = 'The stored procedure finished successfully.', -- 邮件正文
@subject = '样品仓物料清单', --邮件抬头
@execute_query_database = 'UFDATA_001_2016', --查询的数据库
--需要执行的查询
@query = 'select
distinct substring(cinvcode,4,100) 料号
from
CurrentStock
where
cwhcode = 12
and iquantity >=1',
@attach_query_result_as_file = 1,
@query_attachment_filename = 'item.csv'
邮件发送的结果

场景二,用户系统在OA系统完成的外部用户报备客户审批完成后触发邮件给对方。由于OA系统自动触发外部邮件格式有显示,据说需要js写代码,因为不熟悉,所以还是借助于sql server的邮件功能来实现。
预先写一个view,三个字段,需要发送的邮箱,邮件主题,邮件内容。
例子中将主题和主体做为一个,用到循环语句实现。
declare @mail nvarchar(200);
declare @note nvarchar(500);
declare c cursor --游标
for select email,note from cux_dls_notice_v where operatedate + ' '+ operatetime >= DATEADD(MINUTE,-60,GETDATE()) --取最近一小时的记录发送,计划任务是60分钟执行一次。
open c
fetch next from c into @mail,@note;
while @@FETCH_STATUS = 0
begin
EXEC msdb.dbo.sp_send_dbmail
@profile_name= '<账户名>', --定义好的sql server 邮箱账户名
@recipients=@mail, --需要发送的邮箱
@subject=@note, --邮件标题
@body=@note --邮件主题
fetch next from c into @mail,@note;
end
close c;
deallocate c;

场景三,还是在OA系统里,销售申请特价之后提交审批,审批人系统可以收到邮件通知,并在邮件中和销售讨论后,再回到系统中审批。由于申请表的内容多,需要用html的发送格式。
做法和场景二类似,重点是邮件主题需要生成为html的格式。
还是一样把需要展现的内容做成一个view,我个人喜欢做view,这样有什么变化调整view就可以了。
/*声明变量*/
declare @tableHTML varchar(max)
declare @mail nvarchar(200);
declare @note nvarchar(500);
--设置问候词
set @tableHTML = '
您好! 请审批下面的价格申请: |
';
--设置表头
set @tableHTML=@tableHTML
+'
| RFQ No |
sales |
PL3 |
Customer |
disty_name |
2nd disty |
Sold To Customer |
Part No |
Currency |
Volume |
Requested DC |
Customer RP |
Competitor |
Competitor PN |
Competitor Price | ';
--启用游标
declare c cursor for
--查询结果
select
a.email
,a.note
,@tableHTML+'| '+rfq_quotation_number+' | '
+''+lastname+' | '
+''+pl3+' | '
+''+customer+' | '
+''+disty_name+' | '
+''+snd_disty+' | '
+''+sold_to_customer+' | '
+''+fully_part_no+' | '
+''+currency+' | '
+''+volume+' | '
+''+requested_disty_cost+' | '
+''+cust_requested_price+' | '
+''+competitor+' | '
+''+competitor_part_no+' | '
+''+Competitor_Price+' | '
from
(
select
email
,note
,rfq_quotation_number
,lastname
,pl3
,客户中文+'/'+客户英文 as customer
,disty_name
,snd_disty
,sold_to_customer
,fully_part_no
,currency
,isnull(cast(volume as nvarchar(10)),'') volume
,isnull(cast(requested_disty_cost as varchar(10)),'') requested_disty_cost
,isnull(cast(cust_requested_price as varchar(10)),'') as cust_requested_price
,isnull(cast(competitor as varchar(100)),'') competitor
,isnull(cast(competitor_part_no as varchar(50)),'') competitor_part_no
,isnull(cast(competitor_price as varchar(10)),'') competitor_price
from cux_rfq_v
where currentnodetype = 1 and lastoperatedate + ' '+ lastoperatetime >= DATEADD(MINUTE,-60,GETDATE()) --找最近60分的记录,并发送
) a
open c
fetch next from c into
@mail
,@note
,@tableHTML;
while @@FETCH_STATUS = 0
begin
EXEC msdb.dbo.sp_send_dbmail
@profile_name= '<账户名>', --定义好的sql server 邮箱账户名
,@recipients=@mail
,@subject=@note
,@body= @tableHTML
,@body_format='HTML'
fetch next from c into
@mail
,@note
,@tableHTML;
end
close c;
deallocate c;
以上是“SQL数据库msdb.dbo.sp_send_dbmail函数发送邮件的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
分享标题:SQL数据库msdb.dbo.sp_send_dbmail函数发送邮件的示例分析
文章网址:http://www.bluegullmedia.com/article/pgpgdo.html
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-05-17 07:58:22 HTTP/1.1 GET : /article/pgpgdo.html
- 运行时间 : 0.0509s ( Load:0.0012s Init:0.0008s Exec:0.0370s Template:0.0119s )
- 吞吐率 : 19.65req/s
- 内存开销 : 484.77 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 0 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=ehd9127rv8b4h1irol3e2q0q04
- /home/wwwroot/bluegullmedia/wwwroot/index.php ( 1.09 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Mode/common.php ( 2.82 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Common/functions.php ( 53.56 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /home/wwwroot/bluegullmedia/wwwroot/App/Common/Conf/config.php ( 2.16 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Conf/debug.php ( 1.49 KB )
- /home/wwwroot/bluegullmedia/wwwroot/App/Home/Conf/config.php ( 0.31 KB )
- /home/wwwroot/bluegullmedia/wwwroot/App/Home/Common/function.php ( 3.33 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /home/wwwroot/bluegullmedia/wwwroot/App/Home/Controller/ArticleController.class.php ( 6.02 KB )
- /home/wwwroot/bluegullmedia/wwwroot/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /home/wwwroot/bluegullmedia/wwwroot/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 18.20 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000010s ]
- [ app_init ] --END-- [ RunTime:0.000055s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000086s ]
- [ app_begin ] --END-- [ RunTime:0.000129s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000097s ]
- [ template_filter ] --END-- [ RunTime:0.000146s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.008061s ]
- [ view_parse ] --END-- [ RunTime:0.008105s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000764s ]
- [ view_filter ] --END-- [ RunTime:0.000790s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /home/wwwroot/bluegullmedia/wwwroot/App/Home/Controller/ArticleController.class.php 第 47 行.
- [2] mkdir(): Permission denied /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 59 行.
- [2] mkdir(): Permission denied /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 59 行.
- [8] Undefined index: db_host /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /home/wwwroot/bluegullmedia/wwwroot/ThinkPHP/Library/Think/Db.class.php 第 122 行.

0.0509s 
|