Navigation

1/26/2015

Using javascript to create Outlook email message

Want to use HTML to open Outlook with new message, use script below (only in IE):

<script type="text/javascript">
function OpenOutlookDoc(email_address,body_stuff)
{
try
{

var outlookApp = new ActiveXObject("Outlook.Application");
var nameSpace = outlookApp.getNameSpace("MAPI");
mailFolder = nameSpace.getDefaultFolder(6);
mailItem = mailFolder.Items.add('IPM.Note.FormA');
mailItem.Subject="a subject test";
mailItem.To = email_address;
mailItem.HTMLBody = body_stuff;
mailItem.display (0);
}
catch(e)
{
alert(e);
// act on any error that you get
}
}
</script>


<a href="javascript:OpenOutlookDoc('zzz@test.com','Body of message')">Click</a>


On first attempt, got error "Automation server can't create object."   Just need to do following to IE security settings: 

a) Go to Tools-->Internet Options
b) Select security tab
c) Click on Trusted Sites (or Local Intranet depending on whether your site is trusted or not)
d) Click on Custom Level
e) Ensure that "Initialize and script active x controls is not marked safe for scripting" is enabled - this comes under Activex controls and plug-ins section towards 1/4th of the scroll bar.
Click OK, OK.
Once this is completed, clear the browser cookies and cache. Close all your browser sessions. Reopen the IE to launch your site.
Try to disable the setting in step (e) to see if the problem comes back - that should give more insight to the problem.

No comments:

Post a Comment