【问题标题】:SQL Server SP_SEND_DBMAIL Image file attachmentSQL Server SP_SEND_DBMAIL 图像文件附件
【发布时间】:2010-09-13 12:22:55
【问题描述】:

我正在使用表上的触发器通过 sp_send_dbmail 发送电子邮件。

我想在图像类型的电子邮件中包含文件附件。

jpeg 的原始数据存储在二进制类型的 ndl_Image 列中。

我有以下代码:-

DECLARE @ReferenceID varchar(max)
DECLARE @Recipient varchar(Max)
DECLARE @Body varchar(max)
DECLARE @Subject varchar(max)
DECLARE @Q varchar(max)

--Get the EntryId and FormID for the inserted data.
SET @ReferenceID = 40
SET @Recipient = (SELECT ndl_CategorySendTo FROM ndl_config WHERE ndl_CategoryName = 'Dead Animal')
SET @Body = '<html>A new request has been created.</html>'
SET @Subject = 'NDL Report It: New Request #'+@ReferenceID
SET @Q = 'SELECT ndl_Image from dbo.ndl_data where ndl_ID ='+@ReferenceID
--Execute the stored procedure to send mail.
EXEC msdb.dbo.sp_send_dbmail

--Pass it the following paramaters.
@recipients=@Recipient,
@body=@Body, 
@subject=@Subject,
@profile_name='NDLProfile',
@body_format ='HTML',
    @execute_query_database='NDL_MX',
@query = @Q,
@attach_query_result_as_file = 1,
@query_attachment_filename = 'image.jpg'

这工作正常,但如果我注释掉最后一行,似乎会将查询作为文本文件返回。

如何将附件作为 jpeg 文件获取????

谢谢。

【问题讨论】:

    标签: sql-server sp-send-dbmail


    【解决方案1】:

    我认为这是不可能的。如SP_SEND_DBMAIL 的文档中所述:

    "当指定查询时,结果 set 被格式化为内联文本。 结果中的二进制数据被发送到 十六进制格式。"[强调]

    【讨论】:

    • 我认为您可以通过首先使用 BCP 导出到文件系统来实现相同的想法。虽然不如直接从 varbinary 附加那么优雅,但最终目标仍然可以实现。我在这里发布了一些示例代码作为概念:stackoverflow.com/a/12432031/261997
    【解决方案2】:

    下面的示例会将图像嵌入到 标记中,这将适用于 jpeg、png 等。它不能解决 PDF,但至少可以处理图像。

    CREATE TABLE [dbo].[EmailAttachment](
      [EmailAttachmentID] [int] IDENTITY(1,1) NOT NULL,
      [MassEmailID] [int] NULL, -- foreign key
      [FileData] [varbinary](max) NOT NULL,
      [FileName] [varchar](100) NOT NULL,
      [MimeType] [varchar](100) NOT NULL )
    
    -- ================================
    
     declare @eRecipient nvarchar(max) = 'me@example.com';
     declare @eSubject nvarchar(max) = 'Testing!';
     declare @FileName nvarchar(2000);
     declare @MimeType nvarchar(200);
     declare @attachText nvarchar(max);
     declare @eBody nvarchar(max) =  '<html><head><style>table, th, td {border-collapse: collapse;border: 1px solid black;} img {width: 100%; max-width: 640px;}</style></head>' + 
                                 '<body><h1>Data with pics!</h1><table>' + 
                                 '<tr><th>Some text</th><th>Some Pics</th></tr>';
     
    
      declare c1 cursor for
        select FileName,
            MimeType, 
              /* MimeType should be something like 'image/jpeg' or 'image/png' */
            cast('' as xml).value('xs:base64Binary(sql:column("FileData"))', 'varchar(max)') attachText
              /* the above uses XML commands to convert the binary attachment to UUencoded text */
        from EmailAttachment
        order by 1;
    
    open c1;
    fetch next from c1 into @FileName, @MimeType, @attachText
    while @@FETCH_STATUS = 0
    begin
        set @eBody = @eBody + '<tr><td>Filename: ' + @FileName + 
           '</td><td><img src="data:' + @contentType + ';base64,' +
            @AttachText + '="></td></tr>';
        /* note that the img tag contents the encoded image data, the mime type, and that it ends in an = sign. */
        fetch next from c1 into @FileName, @MimeType, @attachText
    end;
    close c1;
    deallocate c1;
    set @eBody = @eBody + '</table></body></html>';
    
    exec msdb.dbo.sp_send_dbmail
        @recipients = @eRecipient, @body = @eBody, @subject = @eSubject, @body_format='HTML'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多