Sample: Use OOM in an HTML page to create an email with an attachment and display it using Outlook.


Some customers use Outlook Object Model in IE.  This example shows how this might be done.  Note that IE security settings need to be opened up in order to make this work.   I wrote the sample below to troubleshoot issues with this type of usage of OOM.

 

To get this page to work you will need to allow active X scripting.  Note that this is considered to be a possible security risk, so its not something I would really recommend doing.

Go to Internet Explorer Options

Choose the Security tab.

Select Local Internet

Select Custom Level

Select ActiveX controls and Plug-Ins

Select Initialize and script ActiveX controls not marked as safe for scripting – set to Enable.

 

Here is a sample page which demonstrates using Outlook from an HTML page:

 

<html>
<head>
<script>
function test()
{

var sInfo = “”;
document.getElementById(“TestResults”).innerHTML = “”;

var StartTime = new Date()
sInfo += “Start:  ” + StartTime + “<br>”;

try {
sInfo += “Create Outlook.Application<br>”;
var outlook = new ActiveXObject(‘Outlook.Application’);

sInfo += “outlook.CreateItem<br>”;
var email = outlook.CreateItem(0);

sInfo += “email.Recipients.Add<br>”;
email.Recipients.Add(‘testuser@contoso.com’).Type = 1; //1=To

//subject and attachments
email.Subject = ‘Javascript test’;

sInfo += “email.Attachments.Add<br>”;
email.Attachments.Add(‘http://contoso/documents/wonderful.pdf’);

sInfo += “email.Display<br>”;
email.Display();

//email.x();  Uncomment to check handling a failure.

}
catch (error)
{

sInfo += “Error(s) found:<br>”;
for (var er in error)
sInfo += “Error:  ” + er + “: ” + error[er] + “<br>”;

}

var EndTime = new Date()
sInfo += “End:  ” + EndTime + “<br>”;

document.getElementById(“TestResults”).innerHTML = sInfo;
}
</script>
</head>
<body>
Test OOM in an html page<br>
————— <br>

<input type=”button” onclick=”test()” value=”Run Test”/>
<br>
————— <br>
<p id=”TestResults”></p>
<br>

</body>

</html>

 

Comments (0)

Skip to main content