admin管理员组

文章数量:1394759

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

Like below (taken from here):

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

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

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

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

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

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

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

A lot of thanks, anyway.

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

Like below (taken from here):

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

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

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

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

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

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

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

A lot of thanks, anyway.

Share Improve this question edited May 23, 2017 at 10:28 CommunityBot 11 silver badge asked Feb 5, 2016 at 16:42 Yuriy N.Yuriy N. 6,1752 gold badges46 silver badges41 bronze badges 6
  • Have you tried using the file protocol for Attachment.Add? ie - 'file:///C:/file.txt' – Scot Commented Feb 5, 2016 at 16:59
  • Side note: Is this for a limited very controlled environment? If not expect it to break... often. (I know from experience) – Michael Hobbs Commented Feb 5, 2016 at 17:07
  • @Michael Hobbs. Yes, it is. – Yuriy N. Commented Feb 5, 2016 at 18:24
  • Now that I think about this, this is likely not possible the way you are trying to do it. Recently, I did some client side file work and it seems that JS is not able to reach out of the sandbox of the browser. What I mean by this is that the user must bring a file into the sandbox before it can be used by JS. On top of that when a file is brought in it does not bring it's path with it. Files will have a path of /name or /folder/name if a folder was brought in. – Michael Hobbs Commented Feb 5, 2016 at 20:31
  • @Michael Hobbs. All that Javascript does, as I understand that, is the passing path to Outlook thru ActiveXControl. So, I don't see here any [security] problem. Probably, wrong. – Yuriy N. Commented Feb 6, 2016 at 12:57
 |  Show 1 more ment

2 Answers 2

Reset to default 3

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.

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 = ('[email protected]');
          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 = ('[email protected]');
          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 


本文标签: Javascript Open Outlook and add attachments to new emailStack Overflow