sp_send_dbmail在正文中嵌入mhtml文件

sp_send_dbmail在正文中嵌入mhtml文件,html,sql,sql-server,ssrs-2012,sp-send-dbmail,Html,Sql,Sql Server,Ssrs 2012,Sp Send Dbmail,我有一个SSRS报告,我需要使用SQL Server中的sp_dbmail存储过程将其嵌入电子邮件正文中。在附加文件时,我可以使用“插入为文本”选项附加SSRS报告的.mhtml导出,从而使用Outlook前端完成此操作 有没有一种方法可以使用sp_dbmail存储过程实现这一点 我使用的是SQL Server 2014标准是的,可以将文件内容读入变量,然后将其传递给sp\u send\u dbmail。以下是您如何做到这一点: declare @htmlBody varchar(max) S

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

有没有一种方法可以使用sp_dbmail存储过程实现这一点


我使用的是SQL Server 2014标准

是的,可以将文件内容读入变量,然后将其传递给
sp\u send\u 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';
这将把
c:\test\test.html
的内容嵌入电子邮件正文。当然,你可以给身体添加更多

更新:


这仅在您正在读取的文件包含HTML内容时有效。如果你想让它为
mhtml
工作,你需要将
mhtml
文件转换为
html
(有关如何将
mhtml
转换为
html
)的详细信息,请参见@Pops发布的答案。

如果人们想知道,这就是我使用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';

谢谢你的回复。当我尝试您建议的内容时,它嵌入了.mhtml文件的文本(就像我在记事本中打开它时看到的一样)。它不会像我在IE中打开文件时那样显示报告。你能发布一个最小的示例
.mhtml
文件(你的报告)吗?您是否设置了
@body\u format='html'
?我知道问题出在哪里了。只有当文件是HTML而不是MHTML时,它才有效。对我来说,它可以嵌入HTML模板,这很有用。