sp_send_dbmail 在正文中嵌入 mhtml 文件
sp_send_dbmail embed mhtml file in body
我有一份 SSRS 报告,我需要使用 SQL 服务器中的 sp_dbmail 存储过程将其嵌入到电子邮件正文中。我可以使用 Outlook 的前端通过在附加文件时使用 "Insert as Text" 选项附加 SSRS 报告的 .mhtml 导出来执行此操作。
有没有一种方法可以使用 sp_dbmail 存储过程来做到这一点?
我正在使用 SQL Server 2014 Standard
是的,可以通过将文件内容读入变量,然后将其传递给 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';
这会将 c:\test\test.html
的内容嵌入到电子邮件正文中。当然,你可以在正文中添加更多。
更新:
仅当您正在阅读的文件包含 HTML 内容时才有效。如果你想让它适用于 mhtml
,你需要将 mhtml
文件转换为 html
(有关如何转换 mhtml
的详细信息,请参阅@Pops 发布的答案至 html
).
如果有人想知道,这就是我使用 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';
我有一份 SSRS 报告,我需要使用 SQL 服务器中的 sp_dbmail 存储过程将其嵌入到电子邮件正文中。我可以使用 Outlook 的前端通过在附加文件时使用 "Insert as Text" 选项附加 SSRS 报告的 .mhtml 导出来执行此操作。
有没有一种方法可以使用 sp_dbmail 存储过程来做到这一点?
我正在使用 SQL Server 2014 Standard
是的,可以通过将文件内容读入变量,然后将其传递给 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';
这会将 c:\test\test.html
的内容嵌入到电子邮件正文中。当然,你可以在正文中添加更多。
更新:
仅当您正在阅读的文件包含 HTML 内容时才有效。如果你想让它适用于 mhtml
,你需要将 mhtml
文件转换为 html
(有关如何转换 mhtml
的详细信息,请参阅@Pops 发布的答案至 html
).
如果有人想知道,这就是我使用 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';