在用户注册,我想发送一封带有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]-->发布于 2020-12-27 20:26:25
您可能会发现另一个有用的解决方案是在"""之前删除s
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上运行的代码。
发布于 2020-12-27 20:19:27
通过添加额外的\解决了这个问题,也就是将o\:更改为o\\:和其他地方。我认为解决方案是微不足道的,只是没有理清思路
https://stackoverflow.com/questions/65464962
复制相似问题