email - 修改 CDO.Message 对象中附件的内容类型

标签 email vbscript content-type email-attachments mhtml

当我尝试在 VBScript 中将 MHTML 文件作为附件添加到电子邮件中时,ContentMediaType 被错误地设置为 “message/rfc822” ( RFC 822 )。据我了解,根据 Microsoft 的说法这是正确的,但根据 RFC 2557 的说法是不正确的其中指出它应该是“multipart/lated”。这是一个问题,因为大多数(如果不是全部)邮件客户端将“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"。如果 Attachment (字符串)以 ".mht"".mhtml 结尾,我需要它是 "multipart/lated"(不区分大小写)。我可以使用以下代码来完成此操作。

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 中的附件定义。

我已经考虑过按照 these instructions 手动添加附件,但是当我调用 Message.Attachments.Item(1).Fields.Update 时,该对象变得未定义。我认为设置附件的 ContentMediaType 会隐式调用它的 FieldsUpdate 方法,我认为这就是造成这种意外行为的原因。

如何解决此问题并发送具有 “multipart/lated” 内容类型的 MHTML 文件,同时保持正确的文件扩展名?

最佳答案

因此,您的问题是,如果附件的内容类型设置为 content-type="message/rfc822",至少某些电子邮件客户端会错误地保存 MHTML 附件。

首先,值得注意的是,您对问题根本原因的分析是有缺陷的。您似乎对 multipart/lated MIME 类型发挥作用的地方感到困惑。事实上,RFC 2557没有声明与 MHTML 附件对应的正文部分必须具有 content-type="multipart/lated"。相反,MIME 多部分/相关是 MHTML 文件本身的内部结构。引用维基百科article :

The content of an MHTML file is encoded as if it were an HTML e-mail message, using the MIME type multipart/related.

即如果您使用文本编辑器打开 MHTML 文件,您应该看到以下内容:

Content-Type: multipart/related; ...

Microsoft 声明 MHTML 文件应在 KB937912 中使用 content-type="message/rfc822" 提供。这正是当您通过 AddAttachment 方法附​​加此类文件时 CDO 默认执行的操作。我相信这种行为在任何方面都不违背 RFC 2557。根据 RFC:

There are a number of document formats... that specify documents consisting of a root resource and a number of distinct subsidiary resources referenced by URIs within that root resource. There is an obvious need to be able to send such multi-resource documents in e-mail [SMTP], [RFC822] messages.

The standard defined in this document specifies how to aggregate such multi-resource documents in MIME-formatted [MIME1 to MIME5] messages for precisely this purpose.

回顾一下,您绝对不应该将 MHTML 附件的内容类型设置为 multipart/lated

虽然 message/rfc822 似乎是使用 MHTML 文件的方式,但它显然会触发您在问题中描述的问题。我使用 Outlook 2010 和 OWA 2010 进行了测试,并且能够重现它。

各种电子邮件客户端用于 MHTML 附件的替代内容类型是 application/octet-streamapplication/x-mimearchive。这两个在我的测试中没有出现问题。

关于email - 修改 CDO.Message 对象中附件的内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15976836/

相关文章:

javascript - 什么是批处理文件中的 WScript?

asp-classic - 如何在VBScript中逐行读取CSV文件

http - 如何在 Go 中使用 application/x-www-form-urlencoded content-type 执行 GET 请求?

ruby-on-rails-3 - Rails 内容类型验证不适用于 Paperclip

ios - AFNetworking 接受所有内容类型

linux - 如何仅 grep 最近匹配的搜索字符串?

vbscript - 如何在visual basic脚本上引用主文件夹

node.js - 使用 Namecheap 电子邮件发送 Nodemailer 电子邮件

c# - 如何修改消息正文 - Mimekit Message

email - 为什么电子邮件客户端需要 SMTP 中继服务器?有必要吗?