SQL Server SP_SEND_DBMAIL Image file attachment

十年热恋 提交于 2019-12-06 02:47:30

问题


I am using a trigger on a table to send an email using sp_send_dbmail.

I want to include a file attachment in the email of an image type.

The raw data for the jpeg is stored in the ndl_Image column which is of type binary.

I have the following code:-

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'

This works ok but seems to return the query as a text file if i comment out the last line.

How can I get the attachment as a jpeg file????

Thanks.


回答1:


I don't think this is possible. As specified in the documentation for SP_SEND_DBMAIL:

"When a query is specified, the result set is formatted as inline text. Binary data in the result is sent in hexadecimal format."[emphasis added]



来源:https://stackoverflow.com/questions/3700318/sql-server-sp-send-dbmail-image-file-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!