Note that there are some explanatory texts on larger screens.

plurals
  1. POModifying the content type of an attachment in a CDO.Message object
    primarykey
    data
    text
    <p>When I try to add an MHTML file as an attachment to an email message in VBScript, the <code>ContentMediaType</code> is incorrectly set to <code>"message/rfc822"</code> (<a href="http://tools.ietf.org/html/rfc822" rel="noreferrer">RFC 822</a>). From what I understand, this is correct according to Microsoft, but is incorrect according to <a href="http://tools.ietf.org/html/rfc2557" rel="noreferrer">RFC 2557</a> which states that it should be <code>"multipart/related"</code>. This is a problem, because most (if not all) mail clients interpret <code>"message/rfc822"</code> as an email message. Since the file extensions <code>".mht"</code> and <code>".mhtml"</code> do not match any valid file extension of an email message, the mail client appends one of <code>".msg"</code>, <code>".eml"</code>, 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.</p> <pre><code>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 </code></pre> <p>When I run this code, <code>Message.Attachments.Item(1).ContentMediaType</code> is set to <code>"message/rfc822"</code>. I need it to be <code>"multipart/related"</code> if <code>Attachment</code> (a string) ends with <code>".mht"</code> or <code>".mhtml"</code> (case-insensitive). I can do this with the following code.</p> <pre><code>If Len(Attachment) &gt;= 4 And InStr(Len(Attachment) - 3, Attachment, ".mht", vbTextCompare) Or Len(Attachment) &gt;= 4 And InStr(Len(Attachment) - 5, Attachment, ".mhtml", vbTextCompare) Then Message.Attachments.Item(1).ContentMediaType = "multipart/related" End If </code></pre> <p>For some unknown reason, this undefines the attachment from <code>Message.Attachments</code>.</p> <p>I've looked at manually adding the attachment per <a href="http://msdn.microsoft.com/en-us/library/exchange/aa488360%28v=exchg.65%29.aspx" rel="noreferrer">these instructions</a>, but when I call <code>Message.Attachments.Item(1).Fields.Update</code>, the object becomes undefined. I think setting the attachments's <code>ContentMediaType</code>, implicitly invokes it's <code>Fields</code>'s <code>Update</code> method which is what I think is responsible for this unexpected behavior.</p> <p>How can I get around this and send an MHTML file with the <code>"multipart/related"</code> content type while maintaining the proper file extension?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload