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

发布于 2024-09-15 10:55:14 字数 1087 浏览 11 评论 0原文

我有一个通过 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 放入消息中,但这不起作用。如何发送带有换行符的纯文本,并验证最终结果是否正确?

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

I have a SQL Server 2008 procedure that sends email via sp_send_dbmail.

I'm using the following code:

  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' ;

My myProfile is set to use the local smtp server, which results in a .EML file in c:\inetpub\mailroot\queue

When I open one of those .eml files (ug - the only thing that can open them is outlook express, looking at them in anything else just shows the body as a base64 encoded blob.) it looks like it's rendering the result as HTML - so I'm not sure if the problem is in the client, or

I've tried putting \n into the message, but that didn't work. How can I send plain text with line breaks, and verify that the end result looks correct?

BTW, I can't actually send the email to test it with real email clients - corp. network is locked down.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可爱咩 2024-09-22 10:55:14

我一直使用 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

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

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

I've always used CHAR(13)+CHAR(10) to create line breaks (which seems to work mixed in with nvarchar values) in TSQL, so try something like this:

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

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

this CHAR(13)+CHAR(10) will work with msdb.dbo.sp_send_dbmail, I send formatted e-mails using that all the time.

两个我 2024-09-22 10:55:14

您实际上并没有插入任何换行符。您可以将它们直接嵌入到 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 包含多于一行,则两者都会引发错误。

You aren't actually inserting any line breaks. You can embed them directly in a string literal in SQL Server as below.

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);

Or a tidier approach might be

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); 

Both will raise an error if myTable contains more than one row as you are assigning the result to a scalar variable.

秋日私语 2024-09-22 10:55:14

正如 Fernando68 上面提到的,如果允许 HTML 电子邮件,请设置 @body_format = 'HTML',然后您可以使用 < code>
用于换行符,并使用从 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' ;

As Fernando68 mentions above, if you're allowed HTML emails, set @body_format = 'HTML', then you can use <br/> for line-breaks and make it as fancy as you want using all the tags that you get from HTML like line-breaks, img, strong, and so on...

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' ;
沫离伤花 2024-09-22 10:55:14

就我而言,带有 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,'')))

In my case, the plain text email with CHAR(13)+CHAR(10) was getting formatted by Outlook that "helpfully" removes extra line breaks (unless it is unchecked in Outlook Options).

In addition, my list of entries would be dynamic, so I put together the STUFF FOR XML PATH that pulled in a concatenated string and simply added a literal empty string with a new line break and then assigned sp_send_dbmail's @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,'')))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文