修改CDO.Message对象中附件的内容类型 [英] Modifying the content type of an attachment in a CDO.Message object

查看:1621
本文介绍了修改CDO.Message对象中附件的内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试添加MHTML文件作为VBScript中电子邮件的附件时, ContentMediaType 被错误地设置为message / rfc822 RFC 822 )。根据我的理解,根据Microsoft,这是正确的,但根据 RFC 2557 ,这是错误的应该是multipart / related。这是一个问题,因为大多数(如果不是全部)邮件客户端将message / rfc822解释为电子邮件。由于文件扩展名mht。mhtml不匹配电子邮件的任何有效文件扩展名,邮件客户端将文件名附加到中的一个。msgeml等。当用户打开附件时,它会作为电子邮件消息打开,并且不正确显示,因为MHTML文件和电子邮件的保存方式不同。

When I try to add an MHTML file as an attachment to an email message in VBScript, the ContentMediaType is incorrectly set to "message/rfc822" (RFC 822). From what I understand, this is correct according to Microsoft, but is incorrect according to RFC 2557 which states that it should be "multipart/related". This is a problem, because most (if not all) mail clients interpret "message/rfc822" as an email message. Since the file extensions ".mht" and ".mhtml" do not match any valid file extension of an email message, the mail client appends one of ".msg", ".eml", etc. to the filename. When a user opens the attachment, it opens as an email message and doesn't display correctly since an MHTML file and an email message are saved differently.

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 设置为 消息/ RFC822。我需要它是multipart / related如果附件(一个字符串)以 .mhtmhtml(不区分大小写)。我可以使用以下代码来执行此操作。

When I run this code, Message.Attachments.Item(1).ContentMediaType is set to "message/rfc822". I need it to be "multipart/related" if Attachment (a string) ends with ".mht" or ".mhtml" (case-insensitive). I can do this with the following code.

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

For some unknown reason, this undefines the attachment from Message.Attachments.

我已经看过手动添加每个这些说明,但是当我打电话给 Message.Attachments.Item(1).Fields.Update ,对象变为未定义。我想设置附件的 ContentMediaType 隐式调用它的 Fields 更新方法,这是我认为是这个意想不到的行为。

I've looked at manually adding the attachment per these instructions, but when I call Message.Attachments.Item(1).Fields.Update, the object becomes undefined. I think setting the attachments's ContentMediaType, implicitly invokes it's Fields's Update method which is what I think is responsible for this unexpected behavior.

我如何解决这个问题,并发送一个带有

How can I get around this and send an MHTML file with the "multipart/related" content type while maintaining the proper file extension?

推荐答案

所以你的问题是如果附件的内容类型设置为 content-type =message / rfc822,则至少有一些电子邮件客户端会错误地保存MHTML附件。

So your problem is that at least some email clients save MHTML attachment incorrectly if content type for the attachment is set as content-type="message/rfc822".

首先,值得注意的是,您对该问题的根本原因的分析是有缺陷的。您似乎感到困惑,因为 multipart / related MIME类型发挥作用。事实上, RFC 2557 并不表示对应于MHTML附件的正文部分必须有 content-type =multipart / related。相反,MIME multipart / related是MHTML文件本身的内部结构。引用维基百科文章

First, it is worth noting that your analysis of a root cause of the issue is flawed. You appear to be confused by where multipart/related MIME type comes into play. As a matter of fact, RFC 2557 does NOT state that body part corresponding to MHTML attachment must have content-type="multipart/related". Instead, MIME multipart/related is internal structure of MHTML file itself. Quoting Wikipedia article:


MHTML文件的内容被编码为使用MIME类型multipart /相关的HTML电子邮件消息。

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

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

I.e. if you open MHTML file with text editor, you should see the following:

Content-Type: multipart/related; ...

Microsoft声明MHTML文件应以内容类型= KB937912 中的message / rfc822。当您通过 AddAttachment 方法附加此类文件时,这正是CDO默认执行的。我认为这样的行为与RFC 2557无关。根据RFC:

Microsoft states that MHTML files should be served with content-type="message/rfc822" in KB937912. This is exactly what CDO does by default when you attach such file via AddAttachment method. I believe such behavior does not contradict RFC 2557 in any way. As per the RFC:


有许多文档格式...指定
文档,包括根资源和一些由该根资源中的URI引用的不同
辅助资源。
显然需要能够在电子邮件[SMTP],[RFC822]消息中发送这样的多资源
文件。

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.

本文档中定义的标准规定了如何聚合MIME格式的[MIME1至MIME5]消息
中的
多资源文档,正是为此目的。

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 / related

To recap, you definitely should not set content type of MHTML attachment to multipart/related.

虽然 message / rfc822 似乎是与MHTML文件一起使用的方式,但它显然会触发您所描述的问题在这个问题。我使用Outlook 2010和OWA 2010进行了测试,并能够复制它。

While message/rfc822 seems to be the way to use with MHTML files, it obviously triggers the problem you described in the question. I tested with Outlook 2010 and OWA 2010, and was able to reproduce it.

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

Alternative content types that are used by various email clients for MHTML attachments are application/octet-stream and application/x-mimearchive. These two didn't exhibit the problem in my tests.

这篇关于修改CDO.Message对象中附件的内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆