Automate the creation of an Outlook message with JScript

Written by Patrick on December 26, 2007 – 1:59 pm -

Every so often I need a way to automatically create an Outlook email message. I could create a C# application to do this but that is usually overkill and takes too much time to maintain the source and executable. Instead I create a simple JScript file that creates the Outlook message and populates it with the necessary information.

To do this create a new file named CreateMessage.js. Paste the code below into the file. Save the file and then at the command line run this command: “cscript CreateMessage.js”. Confirm the Outlook dialog that you want to do this and the new message will appear.

var outlookApp = new ActiveXObject("Outlook.Application");
var outlookNamespace =
	     outlookApp.GetNameSpace("MAPI").CurrentUser;
var mailItem = outlookApp.CreateItem(0);

var olTo = 1;
var olCC = 2;
var recipient = mailItem.Recipients.Add ("User1");
recipient.Type = olTo;
var recipient = mailItem.Recipients.Add ("User2");
recipient.Type = olCC;

mailItem.Subject = "Test Subject";
mailItem.HTMLBody = "Test Body";
mailItem.Display();
outlookNamespace.Logoff;


Tags: ,
Posted in Scripts |

Leave a Comment