Problem creating email with javascript attachment

I initiate email creation by calling the code below and adding an attachment to it.

I want the user to be able to enter the recipient and change the contents of the message, so I do not send it immediately.

Why am I getting a RangeError the second time the method is called?
(The first time it works correctly.)

function NewMailItem(p_recipient, p_subject, p_body, p_file, p_attachmentname)
{
   try 
   {
     var objO = new ActiveXObject('Outlook.Application');
     var objNS = objO.GetNameSpace('MAPI');
     var mItm = objO.CreateItem(0);
     mItm.Display();
     if (p_recipient.length > 0) 
     {
       mItm.To = p_recipient;
     }
     mItm.Subject = p_subject;
     if (p_file.length > 0) 
     {
      var mAts = mItm.Attachments;
      mAts.add(p_file, 1, p_body.length + 1, p_attachmentname);
     }
     mItm.Body = p_body;
     mItm.GetInspector.WindowState = 2;
   } catch(e) 
   { 
     alert('unable to create new mail item'); 
   } 
}

The error occurs on the mAts.add line. Therefore, when he tries to attach a document, he fails.

Also, the file name (p_file) is the http address for the image.

+3
source share
3 answers

IE, Outlook . ?

+3

, :

var objO = new ActiveXObject('Outlook.Application');
var mItm = objO.CreateItem(0);

var mAts   = mItm.Attachments;
var p_file = [
  "http://stackoverflow.com/content/img/vote-arrow-up.png",
  "http://stackoverflow.com/content/img/vote-arrow-down.png"
];
for (var i = 0; i < p_file.length; i++) {
  mAts.add(p_file[i]);
}

, Attachments.Add(). , .

? , , .

+1

mItm.display() mItm.GetInspector.WindowState = 2;

0
source

Source: https://habr.com/ru/post/1703293/


All Articles