2

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 = ('test@gmail.com');
    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 commented out.

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.

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.

Community
  • 1
  • 1
Yuriy N.
  • 4,936
  • 2
  • 38
  • 31
  • Have you tried using the file protocol for Attachment.Add? ie - 'file:///C:/file.txt' – Scot Feb 05 '16 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 Feb 05 '16 at 17:07
  • @Michael Hobbs. Yes, it is. – Yuriy N. Feb 05 '16 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 Feb 05 '16 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. Feb 06 '16 at 12:57
  • @Scot Tried, is not working. – Yuriy N. Feb 07 '16 at 11:07

2 Answers2

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 = ('test@gmail.com');
          theMailItem.Subject = ('test');
          theMailItem.Body = ('test');
          theMailItem.Attachments.Add("C:\\file.txt");
          theMailItem.display();
      }
      catch (err) {
         alert(err.message);
      } 
   }
Abhishek
  • 1,261
  • 1
  • 13
  • 30
Yuriy N.
  • 4,936
  • 2
  • 38
  • 31
  • It saying "Path does not exist. Verify the path is correct." at Attachemts.Add(); Could you please help me regarding this? – Chanikya Jun 19 '19 at 05:09
0
       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