【问题标题】:How can I send plain text email (with line breaks) using sp_send_dbmail?如何使用 sp_send_dbmail 发送纯文本电子邮件(带换行符)?
【发布时间】:2010-08-18 17:05:02
【问题描述】:

我有一个通过 sp_send_dbmail 发送电子邮件的 SQL Server 2008 过程。

我正在使用以下代码:

  set @bodyText = ( select 
                      N'Here is one line of text ' +
                      N'It would be nice to have this on a 2nd line ' +
                      N'Below is some data: ' +
                      N' ' +
                      N' ' +
                      field1 +
                      N' ' +
                      field2 +
                      N' ' +
                      N'This is the last line'
                    from myTable )

    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'myProfile',
        @recipients = @to,
        @body = @bodyText,
        @body_format = 'TEXT',
        @subject = 'Testing Email' ;

我的 myProfile 设置为使用本地 smtp 服务器,这会在 c:\inetpub\mailroot\queue 中生成一个 .EML 文件

当我打开其中一个 .eml 文件时(ug - 唯一可以打开它们的是 Outlook Express,在其他任何地方查看它们只会将正文显示为 base64 编码的 blob。)看起来它正在渲染结果作为 HTML - 所以我不确定问题出在客户端,还是

我尝试将 \n 放入消息中,但没有成功。如何发送带有换行符的纯文本,并验证最终结果是否正确?

顺便说一句,我实际上无法发送电子邮件以使用真正的电子邮件客户端进行测试 - 公司。网络已锁定。

【问题讨论】:

  • 这些答案是否解决了您的问题?
  • @KM - 是的。我正在尝试决定接受哪个 - 你的更优雅,阅读效果更好,但 Martins 也有效。

标签: sql-server sql-server-2008 emacs sp-send-dbmail


【解决方案1】:

我一直使用CHAR(13)+CHAR(10) 在 TSQL 中创建换行符(这似乎与 nvarchar 值混合使用),所以试试这样的方法:

DECLARE @CRLF char(2)
       ,@bodyText nvarchar(max)
       ,@field1  nvarchar(10)
       ,@field2  nvarchar(10)
SELECT @CRLF=CHAR(13)+CHAR(10)
      ,@field1='your data'
      ,@field2='and more'

set @bodyText =
                N'Here is one line of text ' 
                +@CRLF+ N'It would be nice to have this on a 2nd line ' 
                +@CRLF+ N'Below is some data: ' + N' ' + N' ' + ISNULL(@field1,'') + N' ' + ISNULL(@field2 + N' ' ,'')
                +@CRLF+ N'This is the last line' 


PRINT @bodyText

输出:

Here is one line of text 
It would be nice to have this on a 2nd line 
Below is some data:   your data and more 
This is the last line

这个CHAR(13)+CHAR(10) 将与msdb.dbo.sp_send_dbmail 一起使用,我一直使用它发送格式化的电子邮件。

【讨论】:

  • 如果您发送的邮件正文为 HTML,则不会
【解决方案2】:

您实际上并没有插入任何换行符。您可以将它们直接嵌入到 SQL Server 中的字符串文字中,如下所示。

SET @bodyText = (SELECT N'Here is one line of text 
It would be nice to have this on a 2nd line 
Below is some data: 


' + field1 + N' 

' + field2 + N' 

' + N'This is the last line'
                 FROM   myTable);

或者更整洁的方法可能是

DECLARE @Template NVARCHAR(max) = 
N'Here is one line of text 
It would be nice to have this on a 2nd line 
Below is some data: 

##field1##

##field2##

This is the last line';

SET @bodyText = (SELECT REPLACE(
                    REPLACE(@Template, 
                       '##field1##', field1), 
                       '##field2##', field2)
                 FROM   myTable); 

如果myTable 在将结果分配给标量变量时包含多于一行,两者都会引发错误。

【讨论】:

  • 令人惊讶的是,确实如此!事实上,我可以将整个块放入一对引号中。
  • 确实让代码看起来很时髦,我曾经这样做过。但是,我发现CHAR(13)+CHAR(10) 更适用于缩进查询和其他代码。
  • 是的,缩进并不能很好地工作,因为' 需要在行首直接进行,以避免注入大量多余的空间,但它可以看起来更多所见即所得,无需担心。
【解决方案3】:

正如Fernando68 上面提到的,如果您被允许使用 HTML 电子邮件,请设置 @body_format = 'HTML',然后您可以使用 <br/> 进行换行,并使用所有标签使其变得随心所欲您可以从 HTML 中获得诸如换行符、img、strong 等...

set @bodyText = ( select 
                  '<h1>My Data</h1><p>Here is one line of text<br/>
                   It would be nice to have this on a 2nd line <br/>
                   Below is some data: <br/>'
                   + field1 + '<br/>'
                   + field2 + '<br/>'
                   + 'This is the last line</p>'
                   from myTable )

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'myProfile',
    @recipients = @to,
    @body = @bodyText,
    @body_format = 'HTML',
    @subject = 'Testing Email' ;

【讨论】:

  • 这并不能真正回答原始问题,但它可能是最好和最简单的解决方案。我过去曾使用这种方法可靠地解决过这个问题(我找到了一个通常有效但并非总是有效的文本解决方案!)。
【解决方案4】:

在我的例子中,带有CHAR(13)+CHAR(10) 的纯文本电子邮件被 Outlook 格式化,“有用”地删除了额外的换行符(除非在 Outlook 选项中未选中)。

此外,我的条目列表是动态的,所以我将 STUFF FOR XML PATH 放在一起,它拉入一个连接的字符串,并简单地添加一个带有新换行符的文字空字符串,然后分配 sp_send_dbmail 的 @body = @emailBody :

DECLARE @emailBody VARCHAR(MAX)

SET @emailBody = CONCAT('files: '
    ,(SELECT STUFF((SELECT
    ' 

' + CONCAT([FileName],'.csv')
    FROM localdb.dbo.tableName
    for xml path(''), TYPE).value('.', 'VARCHAR(MAX)'),1,1,'')))

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 2010-11-10
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多