如何使用sp_send_dbmail发送纯文本电子邮件(带换行符)?

chr*_*ris 13 sql-server emacs sp-send-dbmail sql-server-2008

我有一个SQL Server 2008程序,通过sp_send_dbmail发送电子邮件.

我正在使用以下代码:

  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' ;
Run Code Online (Sandbox Code Playgroud)

我的myProfile设置为使用本地smtp服务器,这导致c:\ inetpub\mailroot\queue中的.EML文件

当我打开其中一个.eml文件时(ug - 唯一可以打开它们的是outlook express,在其他任何东西中查看它们只是将主体显示为base64编码的blob.)它看起来像是将结果呈现为HTML - 所以我不确定问题是在客户端,还是

我已经尝试将\n放入消息中,但这不起作用.如何发送包含换行符的纯文本,并验证最终结果是否正确?

顺便说一句,我实际上无法发送电子邮件来测试真实的电子邮件客户端 - corp.网络被锁定.

KM.*_*KM. 15

我总是习惯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
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

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
Run Code Online (Sandbox Code Playgroud)

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

  • 如果您要发送正文为 HTML 的电子邮件,则不会 (2认同)

Mar*_*ith 9

您实际上并没有插入任何换行符.您可以将它们直接嵌入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);
Run Code Online (Sandbox Code Playgroud)

或者更整洁的方法

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); 
Run Code Online (Sandbox Code Playgroud)

如果myTable将结果分配给标量变量,则如果包含多行,则两者都会引发错误.


Cli*_*ter 5

正如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' ;
Run Code Online (Sandbox Code Playgroud)