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

查看:132
本文介绍了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 not on other browsers. It opens a 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天全站免登陆