string - StringContext$InvalidEscapeException: 在创建 HTML 字符串主体时,无效转义 '\:' 不是 [\b、\t、\n、\f、\r、\\、\"、\'] 之一
问题描述
在用户注册时,我想向用户发送一封带有html
正文的电子邮件。我创建的电子邮件模板如下:
object EmailHTML {
val body =
s"""
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]-->
...
""".stripMargin
}
我像这样使用它:
val html = if(userToken.tokenType == UserTokenType.RegistrationConfirmation){
EmailHTML.body
}else {
EmailHTML.body
}
SignupEmail(subject,from,html)
当我执行代码时,我收到以下错误:
Caused by: scala.StringContext$InvalidEscapeException: invalid escape '\:' not one of [\b, \t, \n, \f, \r, \\, \", \'] at index 454 in "
|<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head>
...
如果我删除该行,则不会发生异常:
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]-->
但是我收到的电子邮件格式不正确!可能是什么问题,这条线的重要性是什么?
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]-->
解决方案
您可能会发现有用的另一个解决方案是删除s
before """
:
val body1 = """
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]-->
...""".stripMargin
这将导致字符串不被评估,因此它将保持原样。
在Scastie运行的代码。
推荐阅读
- python - 如何在 Qt 应用程序中呈现 Jira 问题
- express - 将获取请求重定向到发布请求?使用快递
- c# - 使用承载身份验证时未发送正文
- c# - 传递给函数(但不是本地)时列表元素的意外不平等
- leaflet - 如何解决 Leaflet TypeError:L.control.selectLayers 不是函数
- sql - 连接 3 个表时重复值
- java - 如果 Parse 服务器本身已关闭, saveEventually() 会起作用吗?
- javascript - 点击图片时如何弹出小窗口?
- java - 验证 char 用户输入循环
- three.js - Three.js:使光线投射器的方向与物体的方向相同