欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL Server中发送HTML格式邮件的方法

程序员文章站 2023-01-03 17:44:25
sql server 发送html格式的邮件,参考代码如下: declare @tablehtml nvarchar(max) ; -- 获取当前系...

sql server 发送html格式的邮件,参考代码如下:

   declare @tablehtml nvarchar(max) ;
   -- 获取当前系统时间,和数据统计的时间
   set @d_nowdate = convert(datetime,convert(varchar(10),dateadd(day,-1,getdate()),120),120);

   -- 如果有数据则发送
    if exists (select top 1 * from t_table1(nolock) where d_rq=@d_nowdate)
   begin
    set @str_subject='某某'+convert(varchar(10),@d_nowdate,120)+'净值.';
    set @tablehtml = n'<h1>某某</h1><br>目前测试中<br><table border="1">' +
         n'<tr><th>日期</th><th>基金代码</th><th>基金名称</th><th>净值</th><th>累计净值</th></tr>' +
       cast ( (select convert(varchar(10),@d_nowdate,120) as 'td','',vc_jjdm as 'td','',vc_jjmc as 'td','',en_jjdwjz as 'td','',en_ljjz as 'td'
          from t_table1 t left join t_table2 tt on t.vc_jjdm = tt.c_fundcode 
         where d_rq=@d_nowdate order by vc_jjmc,vc_jjdm
          for xml path('tr'), elements-- type 
          ) as nvarchar(max) ) + n'</table>';

    -- 发送邮件
    exec @i_result = msdb.dbo.sp_send_dbmail
           @profile_name = 'profile-mail',
           @recipients = '邮箱地址1;邮箱2;邮箱3', 
           @subject = @str_subject,
           @body = @tablehtml,
           @body_format = 'html';
   end

邮件效果如下:

某某净值

目前测试中

日期

基金代码

基金名称

净值

累计净值

2013-12-20

111111

aaaaa

0.98300000

0.98300000

2013-12-20

222222

bbbbb

1.04900000

1.04900000

2013-12-20

333333

ccccc

0.64000000

0.64000000

2013-12-20

444444

ddddd

0.99400000

0.99400000

2013-12-20

555555

eeeee

1.05700000

1.05700000

2013-12-20

666666

fffff

0.73400000

0.73400000