4

I'm initiating an email create, by calling the code below, and adding an attachment to it.

I want the user to be able to type in the receipient, and modify the contents of the message, so I'm not sending it immediately.

Why do I get a RangeError the 2nd 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 is occuring on the mAts.add line. So when it tries to attach the document, it fails.

Also the file name (p_file) is a http address to a image.

3
  • The fact that this is even remotely possible in JavaScript scares me. Commented Feb 16, 2009 at 18:17
  • It's a trusted site, on an intranet, but I know what you mean. Commented Feb 16, 2009 at 19:21
  • 1
    @scunliffe: Everything is possible in JavaScript. I's a programming language after all. In this case, it is JScript and this won't even run in a non-trusted environment. Commented Mar 11, 2009 at 12:59

3 Answers 3

3

Won't work outside of IE, the user needs to have Outlook on the machine and an account configured on it. Are you sure you want to send an email this way?

Sign up to request clarification or add additional context in comments.

10 Comments

That's fine, this is used in a controlled environment where each machine has IE and Outlook installed, and sadly yes, this is what they want.
And this is why 25% of the web is till on crappy IE6, because of reasoning like this.
By coding specifically to IE and outlook, you're participating in an unethical corporate lock-in strategy. Maybe you can live with the future pain and suffering you are causing to all web developers, and users of the system. If I were in your position, I would have to resign.
Oh please. Get off your high horse. You're going to resign over a tiny piece of Javascript? It's not even unethical. It's the corporate policy.
See this thread: stackoverflow.com/questions/657893/… for some other views.
|
1

I'm trying it with this little snippet, and it works flawlessly:

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]);
}

Note that I left off all optional arguments to Attachments.Add(). The method defaults to adding the attachments at the end, which is what you seem to want anyway.

Can you try this standalone snippet? If it works for you, please do a step-by-step reduction of your code towards this absolute minimum, and you will find what causes the error.

8 Comments

Sorry, I definately can. It works the first time. Just not after that.
Wild guess: Can you attach the same document twice? Maybe there is something wrong with the second document?
I am attaching the same document, the error title is RangeError, i'm not sure if I'm going to get more detail then that.
Hm.... "RangeError" is a JavaScript error, so it doesn't even come to the point where Add() is called. That means: Add() doesn't throw the error. Can you try without "p_body.length + 1" (just set that part to 0)?
Also, how do you call your code to add a second attachment? Your code as you have it in the question does not support that, or does it?
|
0

first do mItm.display() then write mItm.GetInspector.WindowState = 2; this will work

1 Comment

I tried this, and unfortunately it doesn't help. The first time is fine, the second time it gets the error above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.