【问题标题】:sp_send_dbmail embed mhtml file in bodysp_send_dbmail 在正文中嵌入 mhtml 文件
【发布时间】:2017-03-17 21:54:30
【问题描述】:

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

有没有办法使用 sp_dbmail 存储过程来做到这一点?

我使用的是 SQL Server 2014 标准版

【问题讨论】:

    标签: html sql sql-server ssrs-2012 sp-send-dbmail


    【解决方案1】:

    是的,可以通过将文件内容读入一个变量,然后将其传递给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 转换为html 的详细信息,请参阅@Pops 发布的答案)。

    【讨论】:

    • 感谢您的回复。当我尝试您的建议时,它会嵌入 .mhtml 文件的文本(就像我在记事本中打开它时看到的那样)。如果我在 IE 中打开文件,它不会显示报告。
    • 你能发布一个最小的例子.mhtml文件(你的报告)吗?你设置@body_format = 'html'了吗?
    • 我知道问题出在哪里。它仅在文件是 HTML 而不是 MHTML 时才有效。
    • 对我来说,它正在嵌入 HTML 模板,这很有用。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多