views:

3253

answers:

4

Does anyone know how to open Outlook using Javascript?

I am getting an exception (in IE6) while using this code:

var outlookApp = new ActiveXObject("Outlook.Application");
+5  A: 

You can definately do this, the code looks like:

var objO = new ActiveXObject('Outlook.Application');     
var objNS = objO.GetNameSpace('MAPI');     
var mItm = objO.CreateItem(0);     
mItm.Display();     
mItm.To = p_recipient;
mItm.Subject = p_subject;
mItm.Body = p_body;     
mItm.GetInspector.WindowState = 2;

p_recipient, p_subject & p_body being variables, passed in.

You need to ensure this is running on a webpage which users trust, as this will cause exceptions otherwise.
That is it needs to be in the right zone in IE, with the right settings configured for that zone.

Bravax
IE only for a start...
annakata
+3  A: 

You cannot open desktop applications through JavaScript for very obvious security reasons. The example you gave uses ActiveX which is a proprietary Microsoft technology only available in Internet Explorer.

If you simply want to open a message composition in a users email client with fields pre-filled, you can use the mailto: hyperlink prefix. This allows you to specify recipients, subject and body. Example:

<a href="mailto:[email protected]?subject=You can specify subject too">

Unless you have a requirement to specifically use Outlook (and I'd suggest you may want to revisit your requirements if that's the case), it is far more desirable to open whatever the default email client the user has set.

roryf
Hi Rory,I have tried <a href="mailto:... but I am unable to send HTML elements through it. Lets say a link(<a>)?any idea?
Wondering
why would you want to do that?
roryf
I guess most modern email clients support http://www.rfc-editor.org/rfc/rfc2368.txt which includes "body", but does NOT allow for HTML. Above all: not all of your visitors will have a configured email client (won't work out of the box for web based email like Gmail). See http://email.about.com/library/misc/blmailto_encoder.htm You might also want to ensure the recipient's address cannot be read by spambots.
Arjan
well, you CAN open desktop applications using Javascript, using the IE-only ActiveX functions, as well as some relaxed security settings. It's not that helpful to just assume he has no good reason for using this method.
nickf
I didn't just assume that, I stated it could be done with ActiveX but that using the standard mailto: was a better idea, which IMO it is.
roryf
A: 

I seem to recall that Outlook registers its own URI scheme (!), so you can actually open it very simply and easily from a hyperlink of the form outlook:, if that's really what you want to do. Unfortunately I don't own a copy of Outlook anymore, and it's been years since I've tried this, so I'm unable to verify that it still works.

Edited to add: Well! The link shows up as a link in the preview, but not in the actual posted answer. In any case, here is some HTML code to clarify my meaning:

<a href="outlook:">Click here to launch Microsoft Outlook</a>
zaphod
I just tested and this doesn't work in Firefox, didn't try IE.
roryf
Not Working in IE too. Probably <a href="outlook:"> doesnt work for outlook 2007.
Wondering
A: 

Rory F sounds like a boring know-it-all; why does he ask the reasons for a requirement only to editorialize.

Zaphod, your solution fulfilled my requirements perfectly (IE6 and Outlook 2003), thank you. Unfortunately I haven't enough of a "rep" to up you.