Forum Discussion
//This example demonstrates how you can work with Microsoft Outlook from
scripts. It will send messages long with attached script code to John Smith,
John Smith Corp., at JohnSmith@JohnSmithCorp.com.
Run the Outlook_Test procedure.
Requirements:
Microsoft Outlook must be installed on your computer.
NOTE: This example adds a new contact (John Smith, John Smith Corp.)
to your address book. Don't forget to remove it later. */
// MS Outlook constants
var
olMailItem = 0;
olByValue = 1;
olContactItem = 2;
function Outlook_Test()
{
var outlook = CreateOutlook()
if (GetContactByName(outlook, "sanjayram", "raja", "optisolbusinesssolution") == null)
AddContact(outlook, "sanjayram", "xxxx", "xxxxx", "sanjayram.r@xxxxx.com");
SendReport(outlook, "sanjayram", "xxxx", "xxxxxxx",
Project.Path + "\\" + "Script\\Outlook_JS.sj");
}
// Initializes MS Outlook
//function CreateOutlook()
//{
//try
//{
//return Sys.ActiveXObject("Outlook.Application");
//}
//catch (exception)
//{
//return Sys.OleObject("Outlook.Application.12");
//}
//}
function CreateOutlook() {
try {
return new ActiveXObject("Outlook.Application");
} catch (exception) {
try {
return new ActiveXObject("Outlook.Application.12");
} catch (exception) {
// Handle the exception here if needed
return null;
}
}
}
// Returns the ContactItem object by user name
function GetContactByName(outlook, firstName, lastName, company)
{
var ns = outlook.GetNamespace("MAPI");
for (var j = 1; j <= ns.AddressLists.Count; j++)
{
var list = ns.AddressLists.Item(j).AddressEntries
for (var i = 1; i <= list.Count; i++)
{
var contact = list.Item(i).GetContact();
if ( contact != null &&
contact.FirstName == firstName &&
contact.LastName == lastName &&
contact.CompanyName == company )
return contact;
}
}
return null;
}
// Adds a new contact
function AddContact(outlook, firstName, lastName, company, email)
{
var contact = outlook.CreateItem(olContactItem);
contact.FirstName = firstName;
contact.LastName = lastName;
contact.CompanyName = company;
contact.Email1Address = email;
contact.Save();
}
// Sends a report to the user
function SendReport(outlook, firstName, lastName, company, fileNameToAttach)
{
var contact = GetContactByName(outlook, firstName, lastName, company);
if (contact == null)
{
Log.Error("User " + firstName + " " + lastName + " was not found in the address book.");
return;
}
// Creates a new message
var mi = outlook.CreateItem(olMailItem);
// Sets the message subject
mi.Subject = "Test Results";
// Sets the message text
mi.Body = "Hello, " + contact.FirstName + "\n" +
"Today my regression test failed.\n" +
"The script is attached.\n\n" +
"Good Luck,\n" +
"Your tester";
// Specifies the destination address
mi.To = contact.Email1Address;
// Attaches the script code
mi.Attachments.Add(fileNameToAttach, olByValue, mi.Body.length, "Script");
mi.Display(); // This line displays the new mail on screen.
// To send the mail, use the Send method
// mi.Send();
}