views:

1429

answers:

5

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.

A: 

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.

Tomalak
Sorry, I definately can. It works the first time. Just not after that.
Bravax
Wild guess: Can you attach the same document twice? Maybe there is something wrong with the second document?
Tomalak
Oh, and what is the exact error message you are getting?
Tomalak
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.
Bravax
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)?
Tomalak
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?
Tomalak
I tried your first solution, and that didn't make a difference. The code is definately raising the error on the add line, as i've added linenumber reporting, so that is the line it's dying on.
Bravax
I call it a second time, by running the javascript method again. I.e. the user is clicking on a link which triggers it a second time.
Bravax
I can call the sample code I posted here multiple times without getting an error. Can you double-check you have valid arguments for Add() in the second function call?
Tomalak
+3  A: 

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?

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.
Bravax
And this is why 25% of the web is till on crappy IE6, because of reasoning like this.
Breton
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.
Breton
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.
Bravax
Sure I would. "It's not even unethical. It's the corporate policy." hahahaha you're joking right? Listen, seriously, it's developers like you that have caused the misery that the web is currently in. It's because of you that corporations can't/won't upgrade to newer versions of browsers. shame.
Breton
See this thread: http://stackoverflow.com/questions/657893/is-it-ethical-to-follow-company-policy-when-you-think-its-wrong/657929#657929 for some other views.
Bravax
Each language that I know of: ASP, Java, JSP, PHP, Rails, etc. all have *much* better ways to handle sending email that hooking into an IE/Outlook only solution based hack. Keep in mind that this also limits portability to Linux, Mac, iPhone, Blackberry, etc.
scunliffe
Also, can anyone confirm that this still works in IE7, and IE8? especially on Vista... or with minimal security settings set?
scunliffe
See the comments on the main question. TBH all I care about is if it can be done in IE6+. All browsers are set to treat this site as a trusted zone. One day soon I aim to try Tomalak's solution.
Bravax
I don't see any reason why it should be sent from client instead of server side script. If your web-application has login system you can even assign each user their own e-mail and if you send the e-mail out using their e-mail as Sender/Reply-To you will almost mimic exact same conditions as if it was sent from their pc. Of course using js you can cut yourself some slack and code limited solution in 30 lines.
Maiku Mori
A: 

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

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

It works only in IE.What about the Firefox?

Please provide solution universally as it is not 100% sure that user will always use IE as active X works only in IE.

Thanks Maneesh Thareja

A: 

I know this an old thread, but with the snippet above, I cannot get the attachment renamed.

mAts.add(p_file, 1, p_body.length + 1, p_attachmentname);

tried a few different combinations but no luck with Microsoft Outlook 2007.

It simply names the attachment same as the file name.

Help please?

surfer_0441
Ask it in a new question! Welcome to stackoverflow
Andreas Bonini