Sending Email From JavaScript using OutLook Automation

1.1          CODE

 

Copy the code in a notepad file and save the file as “testemail.asp” and put it somewhere in IIS and run from there. Make sure all the three attachments are available before you run this code.

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>

<HTML>

<HEAD>

<title>WebForm1</title>

<script language=”javascript”>

function SendAttach()

{

var theApp   //Reference to Outlook.Application

var theMailItem      //Outlook.mailItem

//Attach Files to the email

var attach1 = “c:\\temp\\sheet.xls”

var attach2 = “c:\\temp\\mail.doc”

var attach3 = “c:\\temp\\test.txt”

//Construct the Email including To(address),subject,body

//var recipient

var subject = “Email Using JavaScript”

var msg = “This is a test mail,sent to you using javascript by kushan thakershy”

//Create a object of Outlook.Application

try

{
var theApp = new ActiveXObject(“Outlook.Application”)

var theMailItem = theApp.CreateItem(0) // value 0 = MailItem

//Bind the variables with the email

theMailItem.to = “venkatalakshmi.pingali@wipro.com”

theMailItem.Subject = (subject);

theMailItem.Body = (msg);

theMailItem.Attachments.Add(attach1)

theMailItem.Attachments.add(attach2)

theMailItem.Attachments.add(attach3)

//Show the mail before sending for review purpose

//You can directly use the theMailItem.send() function

//if you do not want to show the message.

theMailItem.display()

}

catch(err)

{

alert(“The following may have cause this error: \n”+

“1. The Outlook express 2003 is not installed on the machine.\n”+

“2. The msoutl.olb is not availabe at the location “+

“C:\\Program Files\\Microsoft Office\\OFFICE11\\msoutl.old on client’s machine “+

“due to bad installation of the office 2003.”+

“Re-Install office2003 with default settings.\n”+

“3. The Initialize and Scripts ActiveX controls not marked as safe is not set to enable.”)

document.write(“<a href=\””+”./testemail.asp”+”\””+”>”+”Go Back”+”</a>”)

}

}

</script>

<meta name=”GENERATOR” content=”Microsoft Visual Studio .NET 7.1″>

<meta name=”CODE_LANGUAGE” content=”Visual Basic .NET 7.1″>

<meta name=”vs_defaultClientScript” content=”JavaScript”>

<meta name=”vs_targetSchema” content=”http://schemas.microsoft.com/intellisense/ie5″&gt;

</HEAD>

<body MS_POSITIONING=”GridLayout”>

<form id=”Form1″ method=”post” runat=”server”>

<INPUT style=”Z-INDEX: 101; LEFT: 200px; WIDTH: 128px; POSITION: absolute; TOP: 144px; HEIGHT: 32px”

type=”button” value=”Button” onclick=”SendAttach()”>

</form>

</body>

</HTML>

 

1.2          Prerequisites

    1. Outlook Express 2003 must be installed on the client’s machine with the default settings.
    2. The “Initialize and Script ActiveX controls not marked as safe” option should be selected as “Enable”.
      Open Internet Explorer, go to Tools, Internet Options.  Click on the security page and make sure the “Local Intranet” zone is selected at the top.  Then click on Custom Level at the bottom.  In the following window you will see  settings for “Initialize and Script ActiveX controls not marked as safe”, select the option “enable” under the same.
    1. Here it is assumed that the client will only send emails on local DNS over intranet. If the client needs to send email on a DNS other then the local DNS over Internet then the settings for the Internet Zone must be changed as mentioned in the section 1.2->b of this document.
    2. Use the system Configuration utility (Msconfig.exe) to examine the services and system startup for third-party applications that might restrict running code in the Office Application.
      For example, Outlook Automation applications may fail because you are running an antivirus program that has “script blocker” features. 

      Alternatively, follow these steps in Outlook to disable third-party add-ins:

1. On the Tools menu, click Options.
2. On the Other tab, click Advanced Options.
3. In the Advanced Options dialog box, click Add-In Manager.
4. Click to clear the check box for any third-party add-in.
5. Restart Outlook.

If this method resolves the problem, contact the third-party antivirus vendor for more information about an update to the antivirus program.

Advertisements
This entry was posted in JavaScript. Bookmark the permalink.

3 Responses to Sending Email From JavaScript using OutLook Automation

  1. Sandeep says:

    great…..

  2. Reda says:

    It’s really a cooll and helpful piece oof information.
    I am glad that yoou juszt shared thiis helpful information with us.

    Please kerp us informed like this. Thznk youu ffor sharing.

  3. Karthik says:

    Hi, It works on IE. Is there any alternate for ActiveXOjects in firefox and chrome to open outlook with attachments…..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s