空间和马车退货正在添加到HTML电子邮件中
我们有一个服务器生成的HTML文件( myfile.html
),该文件嵌入了发送给客户的电子邮件中。多年来,我们一直在使用这种方法,但问题很小。我们通过II6将Windows Server 2012与SMTP服务器一起使用。最近,HTML在电子邮件中倾斜。在检查源文件时,一切看起来都很好。直接打开HTML文件以在浏览器中查看,如您所期望的。这是我们用来将文件读取到内存中以准备电子邮件的代码:
Set objFile = objFSO.OpenTextFile(strFilePath)
Do While objFile.AtEndOFStream <>True
line = objFile.ReadLine
If Instr(1, line, "<table") > 0 And strHeaderWritten = "N" Then
strHeaderWritten = "Y"
strFileContent=strFileContent & strHeader
End If
strFileContent=strFileContent & line
Loop
set objFile = Nothing
然后我们将内容添加到电子邮件中并发送:
strBody = strFileContent
Set objMail = CreateObject("CDO.Message")
Set objMail.Configuration = cdoConfig
objMail.From = strFrom
objMail.ReplyTo = strReplyTo
objMail.To = strTo
objMail.Subject = strSubject
objMail.HTMLBody = strBody
objMail.Fields("urn:schemas:httpmail:importance").Value = strImportance
objMail.Send
其他人是否有这种情况发生在他们身上?
在这个数小时一直在寻找解释。非常感谢您的阅读!
我尝试使用ADO流方法进行电子邮件,但它仍然相同:
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 2 'adTypeText
objStream.CharSet = Application("CharacterSet")
objStream.Open
objStream.LoadFromFile strFilePath
Do While Not objStream.EOS
line = objStream.ReadText(-2)
If Instr(1, line, "<table") > 0 And strHeaderWritten = "N" Then
strHeaderWritten = "Y"
strFileContent=strFileContent & strHeader
End If
If Instr(1, line, "< table") > 0 Then
strFileContent=strFileContent & "<h3>Broken HTML</h3>"
End If
strFileContent=strFileContent & line
Loop
objStream.Close
Set objStream = Nothing
如您所见,我还为我看到的一个持续错误之一添加了检查,我看到了 space
&lt;
和表
之间插入。以这种方式检查输出不会像检查添加空间的文本一样捕获问题。因此,它必须在编写后发生,否则我需要使用正则表达式进行测试。接下来我会尝试的。我仍然在多个电子邮件客户端看到它。这是ADO流的示例后测试:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是CDO中的一个常见问题。我在网上发现了一些问题,即空间被随机插入到HTML体中。
一个答案是使HTML主体不是一个长的字符串,因为CDO然后将插入随机空间,而是自己包含空格,以便CDO不必这样做。
您可以尝试添加
vbcrlf
或仅在您发送的文本中的普通空间。第二个建议对我来说更有意义。这可能是编码问题。这也解释了为什么添加自己的空格可能是解决方法。
反正; CDO允许在发送之前设置CDO.Message对象的编码。
尝试
objmail.bodypart.contenttransferencoding =“引用打印机”
查看是否解决了。This seems to be a common problem in CDO. I've found a few references online to the problem that spaces are randomly inserted into the HTMLbody.
One answer was to make the HTML body not one long string, because CDO will then insert random spaces, but to include whitespace yourself, so that CDO doesn't have to.
You could try adding
VbCrLf
or just plain spaces in the text you're sending.A second suggestion made more sense to me; this can be an encoding problem. That also explains why adding your own whitespace could be a workaround.
Anyway; CDO allows for setting the encoding of the CDO.Message object before sending.
Try
objMail.BodyPart.ContentTransferEncoding = "quoted-printable"
to see if that solves it.问题是Windows使用线路断路和carrage返回。我建议将文本的正文加载并用VBLF替换所有实例,您会发现您不会再有双间隔了。
例如
The issue is windows use of both line break and carrage return. I recommend loading the body of the text and replacing all instances of vbcrlf with just vblf and you will find you wont have the double spacing anymore.
e.g.