当我尝试将 MHTML 文件作为附件添加到 VBScript 中的电子邮件时,它ContentMediaType
被错误地设置为"message/rfc822"
( RFC 822 )。据我了解,这在 Microsoft 中是正确的,但根据RFC 2557是不正确的,该 RFC 2557指出它应该是"multipart/related"
. 这是一个问题,因为大多数(如果不是全部)邮件客户端都将其解释"message/rfc822"
为电子邮件消息。由于文件扩展名".mht"
与".mhtml"
电子邮件消息的任何有效文件扩展名都不匹配,因此邮件客户端会将 、 等之一附加".msg"
到".eml"
文件名。当用户打开附件时,它会作为电子邮件打开并且无法正确显示,因为 MHTML 文件和电子邮件的保存方式不同。
Sub SendEmail(FromAddress, ToAddress, Subject, Body, Attachment)
Call Err.Clear
On Error Resume Next
Schema = "http://schemas.microsoft.com/cdo/configuration/"
Set Configuration = Sys.OleObject("CDO.Configuration")
Configuration.Fields.Item(Schema + "sendusing") = 2
Configuration.Fields.Item(Schema + "smtpserver") = SMTPServer
Configuration.Fields.Item(Schema + "smtpserverport") = 25
Configuration.Fields.Item(Schema + "smtpauthenticate") = 1
' Configuration.Fields.Item(schema + "sendusername") = ""
' Configuration.Fields.Item(schema + "sendpassword") = ""
Call Configuration.Fields.Update
Set Message = Sys.OleObject("CDO.Message")
Set Message.Configuration = Configuration
Message.From = FromAddress
Message.To = ToAddress
Message.Subject = Subject
Message.HTMLBody = Body
If Not IsEmpty(Attachment) Then
'CDO.Message.AddAttachment doesn't set the correct content media type for an MHTML file.
Call Message.AddAttachment(Attachment)
End If
Call Message.Send
End Sub
当我运行此代码时,Message.Attachments.Item(1).ContentMediaType
设置为"message/rfc822"
. "multipart/related"
如果Attachment
(字符串)以".mht"
或".mhtml"
(不区分大小写)结尾,我需要它。我可以用下面的代码做到这一点。
If Len(Attachment) >= 4 And InStr(Len(Attachment) - 3, Attachment, ".mht", vbTextCompare) Or Len(Attachment) >= 4 And InStr(Len(Attachment) - 5, Attachment, ".mhtml", vbTextCompare) Then
Message.Attachments.Item(1).ContentMediaType = "multipart/related"
End If
出于某种未知原因,这取消了从Message.Attachments
.
我已经按照这些说明查看了手动添加附件,但是当我调用时Message.Attachments.Item(1).Fields.Update
,对象变得未定义。我认为设置附件的ContentMediaType
,隐式调用它的Fields
方法Update
,我认为这是造成这种意外行为的原因。
如何解决此问题并发送具有"multipart/related"
内容类型的 MHTML 文件,同时保持正确的文件扩展名?