0

我在 SQL 中有一个小例程,我每天将其用作计划作业,通过电子邮件将某些事件日志消息发送给我。

   DECLARE @CurrentDate DATETIME,
        @Body VARCHAR(MAX),
        @TableHead VARCHAR(MAX),
        @TableTail VARCHAR(MAX)
SET NOCOUNT ON;
SELECT @CurrentDate=DATEADD(dw, -5, GETDATE())      --Three weekdays back
SET @TableTail='</table></body></html>'
SET @TableHead = '<html><head>' +
                  '<style>' +
                  'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:9pt;} ' +
                  '</style>' +
                  '</head>' +
                  '<body><table cellpadding=0 cellspacing=0 border=0>' +
                  '<tr bgcolor=#FFEFD8><td align=center><b>User Name</b></td>' + 
                  '<td align=center><b>Date</b></td>' + 
                  '<td align=center><b>Message</b></td></tr>';
SELECT @Body=(
        SELECT ELogUserName AS [TD],
                CONVERT(NVARCHAR(20),Elogdatetime,101) AS [TD],
                REPLACE(Elogmessage,CHAR(10)+CHAR(13),'<br />') AS [TD]
        FROM tblEventLog
        WHERE ELogDateTime > @CurrentDate AND ELogEventType = 3 AND ELogUsername <> 'jameccccccfer'
        ORDER BY ELogID DESC
        FOR XML RAW('tr'), Elements)

SELECT @Body = @TableHead + @Body + @TableTail

-- return output
INSERT INTO tblPendingMessages
    SELECT 
        'xxxxxxx@Gmail.com' AS Recipient,
        'CPAS Error Log' AS MessageSubject,
        NULL AS MessageCC,
        @Body AS MessageBody,
        @CurrentDate AS MessageGenerated,
        NULL AS MessageSent,
        NULL AS MessageUser,
        'HTML' AS MessageFormat

此代码生成一个 HTML 字符串,如下所示:

<html><head><style>td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:9pt;} </style></head><body><table cellpadding=0 cellspacing=0 border=0><tr bgcolor=#FFEFD8><td align=center><b>User Name</b></td><td align=center><b>Date</b></td><td align=center><b>Message</b></td></tr><tr><TD>nfox</TD><TD>06/29/2012</TD><TD>Error Nbr 3075 - Syntax error (missing operator) in query expression *ProjectID=*.&#x0D;        --&gt; CPASProd.Form_frmWorkOrders.cbProjectID_AfterUpdate, #1, &#x0D; </TD></tr><tr><TD>nfox</TD><TD>06/29/2012</TD><TD>Total number of errors: 1&#x0D; 1. Cannot append.  An object with that name already exists in the collection. (#3367)&#x0D; </TD></tr><tr><TD>nfox</TD><TD>06/29/2012</TD><TD>Total number of errors: 1&#x0D; 1. Cannot append.  An object with that name already exists in the collection. (#3367)&#x0D; </TD></tr><tr><TD>nfox</TD><TD>06/29/2012</TD><TD>Total number of errors: 1&#x0D; 1. Cannot append.  An object with that name already exists in the collection. (#3367)&#x0D; </TD></tr></table></body></html>

不幸的是,当由 Outlook(或更糟,由 gmail Web 客户端...)呈现时,我得到的是 HTML 代码,而不是换行符。

("<br />")

知道我做错了什么吗?

4

0 回答 0