5

我有一个关于使用 sp_send_dbmail 将查询结果作为附件发送的两部分问题。

问题 1:只会打开基本的 .txt 文件。.pdf 或 .jpg 等任何其他格式已损坏。

问题 2:尝试发送多个附件时,我收到一个文件,其中所有文件名都粘在一起。

我正在运行 SQL Server 2005,并且我有一个存储上传文档的表:

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

我还有一个带有标准电子邮件内容的 MassEmail 表。这是 SQL 发送邮件脚本。为简洁起见,我排除了声明语句。

while ( (select count(MassEmailID) from MassEmail where status = 20 )>0) 
begin
    select @MassEmailID = Min(MassEmailID) from MassEmail where status = 20
    select @Subject = [Subject] from MassEmail where MassEmailID = @MassEmailID
    select @Body = Body from MassEmail where MassEmailID = @MassEmailID

    set @query = 'set nocount on; select cast(FileData as varchar(max)) from Mydatabase.dbo.EmailAttachment where MassEmailID = '+ CAST(@MassEmailID as varchar(100))  

    select  @filename = ''
    select  @filename = COALESCE(@filename+ ',', '') +FileName from EmailAttachment where MassEmailID = @MassEmailID

exec msdb.dbo.sp_send_dbmail    
    @profile_name = 'MASS_EMAIL',
    @recipients = 'me@myemail.com',
    @subject = @Subject,
    @body =@Body,
    @body_format ='HTML',
    @query = @query,
    @query_attachment_filename = @filename,
    @attach_query_result_as_file = 1,
    @query_result_separator = '; ',
    @query_no_truncate = 1,
    @query_result_header = 0;

update MassEmailset status= 30,SendDate = GetDate() where MassEmailID = @MassEmailID
end   

我能够成功地从数据库中读取文件,所以我知道二进制数据没有损坏。

.txt 文件仅在我将 FilaData 转换为 varchar 时读取。但显然原始标题丢失了。还值得注意的是,附件文件的大小与原始文件不同。这很可能也是由于编码不当造成的。所以我希望有一种方法可以使用存储的 mimetype 创建文件头,或者以某种方式在二进制数据中包含文件头?

我对最后几个参数的值也没有信心,而且我知道 coalesce 不太正确,因为它在第一个文件名前加上逗号。但是几乎不可能找到好的文档。请帮忙!

4

2 回答 2

8

我不认为您将能够直接从 SQL 发送二进制数据。那里有一些帖子讨论了同样的问题。从Microsoft 文档中,返回到查询的文本被格式化为文本文件。二进制被格式化为十六进制。正如您所指出的,这会损坏任何不是文本文档的文件。

我认为您仍然可以通过首先使用 BCP将二进制数据导出到文件系统,然后通过可用于 sendmail 的传统文件附件方法将其重新导入来完成您想要做的事情。

所以像这样。(仅概念 - 未经测试的代码)

DECLARE @OutputFileAndPath VarChar(500) = '\\Log_Files\MyFile.pdf ' 
DECLARE @sql VarChar(8000)

SELECT @sql = 'BCP "SELECT MyFile FROM [dbo].[MyTable] 
    WHERE PrimaryKey = 12345" queryout ' + @OutputFileAndPath +
        ' -S MyServer\MyInstance -T -fC:\Documents.fmt'

/* you could use a generic format file that would cover most formats */

EXEC xp_cmdshell @sql, NO_OUTPUT;

while ( (select count(MassEmailID) from MassEmail where status = 20 )>0) 
begin
    select @MassEmailID = Min(MassEmailID) from MassEmail where status = 20
    select @Subject = [Subject] from MassEmail where MassEmailID = @MassEmailID
    select @Body = Body from MassEmail where MassEmailID = @MassEmailID


    exec msdb.dbo.sp_send_dbmail    
        @profile_name = 'MASS_EMAIL',
        @recipients = 'me@myemail.com',
        @subject = @Subject,
        @body =@Body,
        @body_format ='HTML',
        @file_attachments = @OutputFileAndPath /* i.e. \\Log_Files\MyFile.pdf */

    update MassEmailset status= 30,SendDate = GetDate() where MassEmailID = @MassEmailID
end     
于 2012-09-14T21:11:26.190 回答
0

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

 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'
于 2021-10-19T16:20:45.123 回答