简体   繁体   English

SQL Server sp_send_dbmail 防止正文中的 html 编码

[英]SQL Server sp_send_dbmail prevent html encoding in body

How can I prevent encoding within the body of an email sent using SQL Server's sp_send_dbmail ?如何防止在使用 SQL Server 的sp_send_dbmail发送的电子邮件正文中进行编码?

Example:例子:

EXEC msdb.dbo.sp_send_dbmail
     @recipients='my.email@somedomain.com',
     @subject = 'SQL email test',
     @body = 'this+that',
     @body_format = 'HTML'

The html source of the email that I receive looks like this:我收到的电子邮件的 html 来源如下所示:

this`+`that

and the body looks fine:身体看起来很好:

this+that

However, what if I want the SOURCE to still have the + instead of the +但是,如果我希望 SOURCE 仍然有 + 而不是+ ? ?

Long back story on why I need to do this, but solving this would help me immensely.关于为什么我需要这样做的长篇大论,但解决这个问题将极大地帮助我。 Is there a way to "escape" the + in my body parameter?有没有办法“转义”我的身体参数中的 + ? Or any other idea on how to prevent this encoding?或者关于如何防止这种编码的任何其他想法?

You can try doing a simple XML hack:您可以尝试做一个简单的 XML hack:

DECLARE @source VARCHAR(255) = 'this`+`that';

DECLARE @body VARCHAR(255)= CAST ( CAST ( 'this`+`that' AS XML ) AS VARCHAR(255) );
SELECT @body AS body;

Returns退货

+-------------+
|    body     |
+-------------+
| this`+`that |
+-------------+

This will not however remove your backticks.但是,这不会删除您的反引号。 You can add a call to REPLACE if needed:如果需要,您可以添加对REPLACE的调用:

DECLARE @body VARCHAR(255)= REPLACE ( CAST ( CAST ( 'this`+`that' AS XML ) AS VARCHAR(255) ), '`', '' );

Which results in:结果是:

+-----------+
|   body    |
+-----------+
| this+that |
+-----------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sp_send_dbmail在主体中嵌入了mhtml文件 - sp_send_dbmail embed mhtml file in body sp_send_dbmail发送HTML而不是HTML格式的电子邮件 - sp_send_dbmail sending HTML instead of HTML formatted email T-SQL; 如何使用sp_send_dbmail以html格式在表格之前和之后添加句子 - T-SQL; How to add sentences before and after a table in an html format using sp_send_dbmail 使用sp_send_dbmail时是否可以在HTML电子邮件中使用sql变量? - Is it possible to use sql variables in HTML email when using sp_send_dbmail? sp_send_dbmail&#39;&lt;&#39;附近的语法不正确 - sp_send_dbmail Incorrect syntax near '<' 通过sp_send_dbmail发送HTML表时,使用超链接对行进行别名处理 - Aliasing rows with hyperlinks when sending HTML table via sp_send_dbmail 使用 sp_send_dbmail 有两个水平对齐的表 - Use sp_send_dbmail to have two horizontally aligned tables 将 CSS 添加到 HTML 生成 email 上的 send_dbmail 上 Z9778840A0100CB30C9828767676BA741 - Add CSS to HTML generated email on send_dbmail on SQL Server 如何在 html 中嵌入图像并通过 msdb.dbo.sp_send_dbmail 将 html 作为 email 发送? - How to embed image in html and send html as email by msdb.dbo.sp_send_dbmail? 将图像插入到SQL Server存储过程中的HTML表中以与DBMail一起使用 - Inserting Image to HTML table within SQL Server stored procedure for use with DBMail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM