Javascript:打开Outlook并将附件添加到新电子邮件 [英] Javascript: Open Outlook and add attachments to new email

查看:981
本文介绍了Javascript:打开Outlook并将附件添加到新电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将附件添加到Outlook中的新电子邮件中.

I am trying to add attachment to new email in Outlook.

如下所示(摘自此处) :

function sendEmail(){
  try{
    var theApp = new ActiveXObject("Outlook.Application");
    var objNS = theApp.GetNameSpace('MAPI');
    var theMailItem = theApp.CreateItem(0); // value 0 = MailItem
    theMailItem.to = ('test@gmail.com');
    theMailItem.Subject = ('test');
    theMailItem.Body = ('test');
    //theMailItem.Attachments.Add("C\\file.txt");
    theMailItem.display();
   }
    catch (err) {
       alert(err.message);
    } 
}

它正在工作(在Outlook中打开并预填充了上述数据的新电子邮件窗口),但是仅当注释了应该添加附件的行时.

It is working (new email window opened in Outlook and prefilled with above data), but only when line that supposed to add attachment is commented out.

如果未注释,则会引发异常,例如找不到文件",但是文件存在.可以将其作为附件手动添加到Outlook中.

In case it is uncommented exception is thrown like "File cannot be found", but file is exist. It can be manually added in Outlook as attachment.

看起来Outlook试图查找文件,但是由于某种原因而无法. 我尝试使用正斜杠,反斜杠和双反斜杠-真不走运.

It looks like Outlooks trying to find file but cannot for some reason. I tried with forward slash, backslash, and double backslash -- no luck.

在Windows 7和8中测试,结果相同. 它只需要在IE上运行即可.

Tested in Windows 7 and 8 with same result. It required to work only from IE.

也许有人可以提供对以上代码的修复,或者有可以在Outlook中添加附件的有效代码段?

Maybe somebody can provide fix to above code or have working piece of code that adds attachment to Outlook?

还是知道某些需要更改的IE或Outlook设置?

Or may be aware of some IE or Outlook settings which need to be changed?

反正非常感谢.

推荐答案

实际上我的路径错误,因此下面的代码可以正常使用.它在Windows 8和IE 11上进行了测试.

Actually I had a wrong path, so the code below is fully working and can be used. It tested on Windows 8 and IE 11.

确定它只能在IE中运行,并且会打开弹出窗口询问运行ActiveX的权限.

Sure it will work only in IE and it opens popup window asking about permission to run ActiveX.

   function sendEmail(){
       try{
          var theApp = new ActiveXObject("Outlook.Application");
          var objNS = theApp.GetNameSpace('MAPI');
          var theMailItem = theApp.CreateItem(0); // value 0 = MailItem
          theMailItem.to = ('test@gmail.com');
          theMailItem.Subject = ('test');
          theMailItem.Body = ('test');
          theMailItem.Attachments.Add("C\\file.txt");
          theMailItem.display();
      }
      catch (err) {
         alert(err.message);
      } 
   }

这篇关于Javascript:打开Outlook并将附件添加到新电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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