我有一个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' ;

我的MyProfile设置为使用本地SMTP服务器,该服务器在C: inetpub mailRoot queue中产生.eml文件。

当我打开其中一个.eml文件(ug-唯一可以打开它们的是Outlook Express,在其他任何内容中都将其视为Base64编码斑点。因此,我不确定问题是否在客户中,或者

我尝试将 n放入消息中,但这无效。我如何将纯文本带有线路断裂,并验证最终结果看起来正确吗?

顺便说一句,我实际上无法发送电子邮件与真实的电子邮件客户端-Corp进行测试。网络被锁定。

有帮助吗?

解决方案

您实际上并没有插入任何线路休息。如下所示,您可以将它们直接嵌入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) 要在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, ,我一直使用该电子邮件发送格式化的电子邮件。

聚会迟到,但 Fernando68 上面提到,如果允许您使用HTML电子邮件,请设置@bodyformat ='html',则可以使用 <br/> 对于线路断路,并尽可能地使用您从HTML获得的所有标签,例如破线,IMG,强壮等...

  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' ;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top