Javascript:打开Outlook并向新电子邮件添加附件

Javascript: Open Outlook and add attachments to new email

本文关键字:电子邮件 添加 打开 Outlook Javascript      更新时间:2023-09-26

我正在尝试将附件添加到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中打开并预填充上述数据的新电子邮件窗口),但仅当应该添加附件的行被注释掉时。

如果是未注释的,则会抛出异常,例如"找不到文件",但文件存在。它可以作为附件手动添加到Outlook中。

看起来Outlook试图查找文件,但由于某种原因无法查找。我尝试了正斜杠,反斜杠和双反斜杠 - 没有运气。

在 Windows 7 和 8 中测试,结果相同。它只需要从IE工作。

也许有人可以提供对上述代码的修复或具有将附件添加到Outlook的工作代码?

或者可能知道一些需要更改的IE或Outlook设置?

无论如何,非常感谢。

实际上我有一条错误的路径,所以下面的代码完全有效并且可以使用。它在Windows 8和IE 11上进行了测试。

当然,它只能在IE中工作,而不能在其他浏览器上工作。它会打开一个弹出窗口,询问运行 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);
      } 
   }
       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);
      } 
   }
semi colon is missing in the path