sp_send_dbmail在主体中嵌入了mhtml文件

Pop*_*ops 12 html sql sql-server sp-send-dbmail ssrs-2012

我有一个SSRS报告,我需要使用SQL Server中的sp_dbmail存储过程将其嵌入到电子邮件正文中.我可以使用Outlook的前端通过在附加文件时使用"作为文本插入"选项附加SSRS报告的.mhtml导出来执行此操作.

有没有办法使用sp_dbmail sproc来做到这一点?

我正在使用SQL Server 2014 Standard

aho*_*xha 7

是的,可以通过将文件的内容读入变量,然后将其传递给sp_send_dbmail.这是你如何做到的:

declare @htmlBody varchar(max)

SELECT @htmlBody=BulkColumn
FROM   OPENROWSET(BULK N'c:\test\test.html',SINGLE_BLOB) x;



EXEC msdb.dbo.sp_send_dbmail
    @profile_name = N'Email', -- you should use the profile name of yours, whatever is set up in your system.
    @recipients = 'recipient_email_id',
    @subject = 'Test',
    @body = @htmlBody,
    @body_format = 'html',
    @from_address = 'sender_email_id';
Run Code Online (Sandbox Code Playgroud)

这将把内容嵌入c:\test\test.html到电子邮件的正文中.当然,你可以添加更多的身体.

更新:

仅当您正在阅读的文件包含HTML内容时,此方法才有效.如果你想它的工作mhtml,你需要将转换mhtml文件html(见@Pops发布关于如何转换细节的答案mhtmlhtml).


Pop*_*ops 5

如果人们想知道,这就是我使用 SQL 将 mhtml 转换为 html 的方法。

declare @source varchar(max), 
@decoded varchar(MAX)

SELECT @source =BulkColumn
FROM   OPENROWSET(BULK N'c:\test\test.mhtml',SINGLE_BLOB) x;

SET @source = SUBSTRING(@source,CHARINDEX('base64',@source,1)+10,LEN(@source))
SET @source = SUBSTRING(@source,1,CHARINDEX('-',@source,CHARINDEX('base64',@source,1)+10)-5)
SET @decoded = cast('' AS xml).value('xs:base64Binary(sql:variable("@source"))', 'varbinary(max)')

EXEC msdb.dbo.sp_send_dbmail
@profile_name = N'Email', -- you should use the profile name of yours, whatever is set up in your system.
@recipients = 'recipient_email_id',
@subject = 'Test',
@body = @decoded,
@body_format = 'html',
@from_address = 'sender_email_id';
Run Code Online (Sandbox Code Playgroud)