Using AddEmail in JScript projects

<< Click to Display Table of Contents >>

Navigation:  Samples and Tutorials > JScript >

Using AddEmail in JScript projects

 

AddEmail ActiveX can be used with any environment that supports JScript. Windows has built-in JScript support and can execute JScript files that have .JS extension. To use AddEmail ActiveX from JScript code create SmtpMail object and send a message using SimpleSendScriptable, SimpleSendAttachmentScriptable or SendScriptable. Please use code snapshots below to get started.

 

Snapshot 1: simple text email.

 

var objSmtpMail, strError, numResultCode;

objSmtpMail = new ActiveXObject("AddEmail.SmtpMail");

objSmtpMail.SmtpServer = "mail.myserver.com";

objSmtpMail.SmtpUsername = "jsmith";

objSmtpMail.SmtpPassword = "mypassword";

numResultCode = objSmtpMail.SimpleSendScriptable("jsmith@myserver.com", "jane@someserver.com;james@someserver.com", "test", "Test message", strError);

if (numResultCode == 0)

{

   WScript.Echo("Sent successfully!");

}

else

{

   strError = objSmtpMail.GetLastErrorDescription();

   WScript.Echo(strError);

}

 

Snapshot 2: text email with attachments.

 

var objSmtpMail, strError, numResultCode;

objSmtpMail = new ActiveXObject("AddEmail.SmtpMail")

objSmtpMail.SmtpServer = "mail.myserver.com";

objSmtpMail.SmtpUsername = "jsmith";

objSmtpMail.SmtpPassword = "mypassword";

numResultCode = objSmtpMail.SimpleSendAttachmentScriptable("jsmith@myserver.com", "jane@someserver.com;james@someserver.com", "test", "Test message", "c:\\files\\doc1.pdf;c:\\files\\doc2.pdf", strError);

if (numResultCode == 0)

{

   WScript.Echo("Sent successfully!");

}

else

{

   strError = objSmtpMail.GetLastErrorDescription();

   WScript.Echo(strError);

}

 

Snapshot 3: HTML message with attachments.

 

var objSmtpMail, objMailMessage, objMailAttachment, objMailAddress, strError, numResultCode

objSmtpMail = new ActiveXObject("AddEmail.SmtpMail");

objSmtpMail.SmtpServer = "mail.myserver.com";

objSmtpMail.SmtpUsername = "jsmith";

objSmtpMail.SmtpPassword = "mypassword";

objSmtpMail.SmtpSSL = false;

 

// Create message and setup subject and body

objMailMessage = new ActiveXObject("AddEmail.MailMessage");

objMailMessage.MessageBodyFormat = 1; //HTML format

objMailMessage.MessageSubject = "test";

objMailMessage.MessageBody = "<html><body><b>Testing...</b></body></html>";

 

// Add first attachment

objMailAttachment = new ActiveXObject("AddEmail.MailAttachment");

objMailAttachment.File = "c:\\files\\doc1.pdf";

objMailMessage.AddAttachment(objMailAttachment);

 

// Add second attachment

objMailAttachment = new ActiveXObject("AddEmail.MailAttachment");

objMailAttachment.File = "c:\\files\\doc2.pdf";

objMailMessage.AddAttachment(objMailAttachment);

 

// Setup sender

objMailAddress = new ActiveXObject("AddEmail.MailAddress");

objMailAddress.Name = "John Smith";

objMailAddress.Address = "jsmith@myserver.com";

objMailMessage.Sender = objMailAddress;

 

// Setup first recipient

objMailAddress = new ActiveXObject("AddEmail.MailAddress");

objMailAddress.Name = "Jane Smith";

objMailAddress.Address = "jane@someserver.com";

objMailMessage.AddRecipient(objMailAddress);

 

// Setup second recipient

objMailAddress = new ActiveXObject("AddEmail.MailAddress");

objMailAddress.Name = "James Smith";

objMailAddress.Address = "james@someserver.com";

objMailMessage.AddRecipient(objMailAddress);

 

// Send prepared message synchronously

numResultCode = objSmtpMail.SendScriptable(objMailMessage, true, strError);

if (numResultCode == 0)

{

   WScript.Echo("Sent successfully!");

}

else

{

   strError = objSmtpMail.GetLastErrorDescription();

   WScript.Echo(strError);

}

 

 

Please refer to the Reference section of this manual for detailed description of AddEmail objects, methods and properties. Included JScript samples provide code snapshots for common operations such as sending text e-mails, sending HTML e-mails, adding attachments to e-mails.