简体   繁体   English

在Javascript中创建带附件的电子邮件时出现问题

[英]Problem creating an email with an attachment in Javascript

I'm initiating an email create, by calling the code below, and adding an attachment to it. 我正在通过调用下面的代码启动电子邮件创建,并为其添加附件。

I want the user to be able to type in the receipient, and modify the contents of the message, so I'm not sending it immediately. 我希望用户能够输入收据,并修改邮件的内容,所以我不会立即发送。

Why do I get a RangeError the 2nd time the method is called? 为什么第二次调用方法时会得到RangeError?
(The first time it works correctly.) (第一次正常工作。)

function NewMailItem(p_recipient, p_subject, p_body, p_file, p_attachmentname)
{
   try 
   {
     var objO = new ActiveXObject('Outlook.Application');
     var objNS = objO.GetNameSpace('MAPI');
     var mItm = objO.CreateItem(0);
     mItm.Display();
     if (p_recipient.length > 0) 
     {
       mItm.To = p_recipient;
     }
     mItm.Subject = p_subject;
     if (p_file.length > 0) 
     {
      var mAts = mItm.Attachments;
      mAts.add(p_file, 1, p_body.length + 1, p_attachmentname);
     }
     mItm.Body = p_body;
     mItm.GetInspector.WindowState = 2;
   } catch(e) 
   { 
     alert('unable to create new mail item'); 
   } 
}

The error is occuring on the mAts.add line. mAts.add行发生错误。 So when it tries to attach the document, it fails. 因此,当它尝试附加文档时,它会失败。

Also the file name (p_file) is a http address to a image. 文件名(p_file)也是图像的http地址。

Won't work outside of IE, the user needs to have Outlook on the machine and an account configured on it. 无法在IE之外工作,用户需要在计算机上安装Outlook并在其上配置帐户。 Are you sure you want to send an email this way? 您确定要以这种方式发送电子邮件吗?

I'm trying it with this little snippet, and it works flawlessly: 我正在尝试使用这个小片段,它完美无缺:

var objO = new ActiveXObject('Outlook.Application');
var mItm = objO.CreateItem(0);

var mAts   = mItm.Attachments;
var p_file = [
  "http://stackoverflow.com/content/img/vote-arrow-up.png",
  "http://stackoverflow.com/content/img/vote-arrow-down.png"
];
for (var i = 0; i < p_file.length; i++) {
  mAts.add(p_file[i]);
}

Note that I left off all optional arguments to Attachments.Add() . 请注意,我中断了Attachments.Add()所有可选参数。 The method defaults to adding the attachments at the end, which is what you seem to want anyway. 该方法默认在最后添加附件,这是您似乎想要的。

Can you try this standalone snippet? 你能试试这个独立的片段吗? If it works for you, please do a step-by-step reduction of your code towards this absolute minimum, and you will find what causes the error. 如果它适合您,请逐步将代码减少到这个绝对最小值,您将找到导致错误的原因。

first do mItm.display() then write mItm.GetInspector.WindowState = 2; 先做mItm.display()然后写mItm.GetInspector.WindowState = 2; this will work 这会奏效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM