如何使用 sp_send_dbmail 发送纯文本电子邮件(带换行符)?
我有一个通过 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我一直使用
CHAR(13)+CHAR(10)
在 TSQL 中创建换行符(似乎与 nvarchar 值混合使用),因此请尝试如下操作:OUTPUT:
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:OUTPUT:
this
CHAR(13)+CHAR(10)
will work withmsdb.dbo.sp_send_dbmail
, I send formatted e-mails using that all the time.您实际上并没有插入任何换行符。您可以将它们直接嵌入到 SQL Server 中的字符串文字中,如下所示。
或者一种更简洁的方法可能是,
如果在将结果分配给标量变量时
myTable
包含多于一行,则两者都会引发错误。You aren't actually inserting any line breaks. You can embed them directly in a string literal in SQL Server as below.
Or a tidier approach might be
Both will raise an error if
myTable
contains more than one row as you are assigning the result to a scalar variable.正如 Fernando68 上面提到的,如果允许 HTML 电子邮件,请设置 @body_format = 'HTML',然后您可以使用 < code>
用于换行符,并使用从 HTML 获得的所有标签(如换行符、img、strong 等)使其变得像您想要的那样精美......
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...就我而言,带有
CHAR(13)+CHAR(10)
的纯文本电子邮件由 Outlook 进行格式化,“有助于”删除多余的换行符(除非在 Outlook 选项中未选中它)。此外,我的条目列表将是动态的,因此我将 STUFF FOR XML PATH 放在一起,该路径拉入连接字符串,并简单地添加带有新换行符的文字空字符串,然后分配 sp_send_dbmail 的 @body = @emailBody:
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: