发送存储在电子邮件正文图像字段中的图像 sp_dbsend_mail

Send image stored in image field in body of Email sp_dbsend_mail

数据库 table 有一个存储为 image 字段的图像 - 我是否能够从数据库中提取该图像,即作为存储过程中的变量提取,或者我是否必须使用类似Crystal 报告?

我想将它包含在已发送的电子邮件中,但不确定是否可能 - 现在想要完整的解决方案(如果可能的话)只是一些正确方向的指示

”澄清-
我有一个 select 查询,它通过光标 运行 完成,并在交付完成后向客户发送电子邮件。这 运行s 每 5 分钟一次 - 我可以扩展查询以包含我找到的图像列,但不确定如何将其显示为电子邮件正文中的图像(图像是捕获的签名) 这是 运行 直接在服务器上,如果更简单,作为附件发送的图像就可以了。 –“

提前致谢

保罗

查看 sp_send_dbmail 的文档,具体参数如下:


[ @query = ] 'query'

Is a query to execute. The results of the query can be attached as a file, or included in the body of the e-mail message. The query is of type nvarchar(max), and can contain any valid Transact-SQL statements. Note that the query is executed in a separate session, so local variables in the script calling sp_send_dbmail are not available to the query.


[ @execute_query_database = ] 'execute_query_database'

Is the database context within which the stored procedure runs the query. The parameter is of type sysname, with a default of the current database. This parameter is only applicable if @query is specified.


[ @attach_query_result_as_file = ] attach_query_result_as_file

Specifies whether the result set of the query is returned as an attached file. attach_query_result_as_file is of type bit, with a default of 0.

When the value is 0, the query results are included in the body of the e-mail message, after the contents of the @body parameter. When the value is 1, the results are returned as an attachment. This parameter is only applicable if @query is specified.


[ @query_attachment_filename = ] query_attachment_filename

Specifies the file name to use for the result set of the query attachment. query_attachment_filename is of type nvarchar(255), with a default of NULL. This parameter is ignored when attach_query_result is 0. When attach_query_result is 1 and this parameter is NULL, Database Mail creates an arbitrary filename.


因此进行查询选择适当的图像,并将其传递给 @query 参数。为其他参数设置适当的值。如果其他查询参数适用于您的情况,请查看文档。

所以我终于对它进行了排序并使用我的其他代码 - 最终能够通过光标导出并在完成后删除文件夹内容 - 同时 dbsendmail 添加了附件我也能够嵌入到正文中电子邮件 - 任何感兴趣的人的代码如下! - 我知道它远非完美,所以感谢任何清理方面的帮助 - 真正的新手警报!!!希望我试图做的事情有意义 - @TT 感谢你的努力,并对 a - 不清楚 - b - 不清楚 - c - blah blah 表示抱歉! - BR 保罗

DECLARE @IMG_PATH VARBINARY(MAX),
@TIMESTAMP VARCHAR(MAX),
@ObjectToken INT,
@MYdtDate NVARCHAR(50),
@MYemail NVARCHAR(50),
@MYszBlockCapitols NVARCHAR(50),
@MYdwJobnumber NVARCHAR(50),
@MYszDelpostCode NVARCHAR(50),
@MYszDelAddrName NVARCHAR(50),
@MYszJobOrderNum NVARCHAR(50),
@MYszDelAddr4 NVARCHAR(50),
@MYszAccCode NVARCHAR(20),
@MYdtTime NVARCHAR(10),
@body NVARCHAR(4000)



DECLARE C1 CURSOR READ_ONLY
FOR
SELECT ISNULL(JobItem.dwJobNumber,' '), ISNULL(JobItem.szDelAddrName,' '), ISNULL(JobItem.szDelPostCode,' '), ISNULL(Signature.szBlockCapitols,' '), ISNULL(StirlingManager_Comp1.dbo.SalesAccounts.szReportText8,' '), Convert(varchar,Signature.dtDate,103) AS Date, Convert(varchar,Signature.dtDate,108)as Time, ISNULL(JobItem.szJobOrderNum,' '), ISNULL(JobItem.szDelAddr4,' '), ISNULL(JobItem.szAccCode,' '), ISNULL(dms_document.data,' ')
FROM         Transport_Comp1.dbo.JobItem INNER JOIN
                      Transport_Comp1.dbo.dms_document ON JobItem.dwJobItemId = dms_document.dwJobItemId LEFT OUTER JOIN
                      Transport_Comp1.dbo.Signature ON dms_document.id = Signature.dwDocumentIdFK LEFT OUTER JOIN
                      DocArchive_Comp1.dbo.dms_document AS dms_document_1 ON dms_document.id = dms_document_1.id LEFT OUTER JOIN
                      StirlingManager_Comp1.dbo.SalesAccounts ON JobItem.szAccCode = StirlingManager_Comp1.dbo.SalesAccounts.szAccountCode
WHERE     (Signature.dtDate > DATEADD(MINUTE, -800, GETUTCDATE())) AND (Signature.dtDate < DATEADD(SECOND, -0.001, GETUTCDATE())) AND (Signature.bArrived = 0) AND (StirlingManager_Comp1.dbo.SalesAccounts.szReportText8 > '')
OPEN C1

FETCH NEXT FROM C1 INTO
 @MYdwJobnumber, @MYszDelAddrName, @MYszDelPostCode, @MYszBlockCapitols, @MYemail, @MYdtDate, @MYdtTime,@MYszJobOrderNum, @MYszDelAddr4, @MYszAccCode, @IMG_PATH
WHILE (@@FETCH_STATUS = 0)
BEGIN
        SET @TIMESTAMP = 'd:\Signatures-TMP\' + @MYdwJobnumber + '.bmp'
        EXEC sp_OACreate 'ADODB.Stream', @ObjectToken OUTPUT
        EXEC sp_OASetProperty @ObjectToken, 'Type', 1
        EXEC sp_OAMethod @ObjectToken, 'Open'
        EXEC sp_OAMethod @ObjectToken, 'Write', NULL, @IMG_PATH
        EXEC sp_OAMethod @ObjectToken, 'SaveToFile', NULL, @TIMESTAMP, 2
        EXEC sp_OAMethod @ObjectToken, 'Close'
        EXEC sp_OADestroy @ObjectToken


SET @body='<html><body><p><img src="http://www.mycompany.co.uk/wp-content/uploads/2013/12/mycompany.png" alt="" align="left"></p>
<br><br><br>
<p><span style="font-size: 9px; font-family: Verdana, Geneva, sans-serif; color: #0d3172;"><strong>My Company<br><span style="Century Gothic;">My Company address</span></span></p>
<br><br></strong>
<h1><span style="font-size: x-large; font-family: arial, helvetica, sans-serif; color: #0d3172;">DELIVERY CONFIRMATION</span></h1>
<br>
<table style="border: 2px double #0d3172; color: #0d3172; font-size: 12pt; font-family: arial, helvetica, sans-serif; width: 800px;" border="1" cellspacing="0" cellpadding="4">
    <tbody>
    <tr>
        <td><span style="font-size: small;">YOUR REFERENCE</span></td>
        <td width="590"><span style="font-size: small;"><strong>' + @MYszJobOrderNum + '</strong></span></td>
    </tr>
    <tr>
        <td><span style="font-size: small;">DELIVERY TO</span></td>
        <td><span style="font-size: small;"><strong>' + @MYszDelAddrName + '</strong></span></td>
    </tr>
    <tr>
        <td><span style="font-size: small;">TOWN</span></td>
        <td><span style="font-size: small;"><strong>' + @MYszDelAddr4 + '</strong></span></td>
    </tr>
    <tr>
        <td><span style="font-size: small;">POSTCODE</span></td>
        <td><span style="font-size: small;"><strong>' + @MYszDelPostCode + '</strong></span></td>
    </tr>
    <tr>
        <td><span style="font-size: small;">DELIVERY DATE</span></td>
        <td><span style="font-size: small;"><strong>' + @MYdtDate + '</strong></span></td>
    </tr>
    <tr>
        <td><span style="font-size: small;">DELIVERY TIME</span></td>
        <td><span style="font-size: small;"><strong>' + @MYdtTime + '</strong></span></td>
    </tr>
    <tr>
        <td><span style="font-size: small;">SIGNED FOR BY</span></td>
        <td><span style="font-size: small;"><strong>' + @MYszBlockCapitols + '</strong></span></td>
    </tr>
    <tr>
        <td><span style="font-size: small;">YOUR ACCOUNT CODE</span></td>
        <td><span style="font-size: small;"><strong>' + @MYszAccCode + '</strong></span></td>
    </tr>
    <tr>
        <td><span style="font-size: small;">OUR JOB NUMBER</span></td>
        <td><span style="font-size: small;"><strong>' + @MYdwJobnumber + '</strong></span></td>
    </tr>
    </tbody>
</table>
<br><br>

<p><span style="font-size: 12px; font-family: Verdana, Geneva, sans-serif; color: #0d3172;"><strong>DIGITAL SIGNATURE</p></strong></span>
<img src="cid:'+ @MydwJobNumber + '.bmp" /><p></p>

<p><span style="font-size: 12px; font-family: Verdana, Geneva, sans-serif; color: #0d3172;"><strong>Please contact our Customer Services Team if you have any questions or queries.</span></strong></span></p></body>
</html>'
                EXEC msdb.dbo.sp_send_dbmail
                @profile_name = 'SQL_Email',
                @recipients = @MYemail,
                @subject = 'EMAIL POD',            
                @file_attachments = @TIMESTAMP,
                @body=@body, 
                @body_format = 'HTML';
                print @body;    

      FETCH NEXT FROM C1 INTO
 @MYdwJobnumber, @MYszDelAddrName, @MYszDelPostCode, @MYszBlockCapitols, @MYemail, @MYdtDate, @MYdtTime,@MYszJobOrderNum, @MYszDelAddr4, @MYszAccCode, @IMG_PATH
END
EXEC xp_cmdshell 'del D:\Signatures-TMP\*.bmp'
CLOSE C1
DEALLOCATE C1