Using AddEmail in ASP / JScript projects

<< Click to Display Table of Contents >>

Navigation:  Samples and Tutorials > ASP - JScript >

Using AddEmail in ASP / JScript projects

 

To use AddEmail in your ASP / JScript project perform the following steps:

 

1. (Optional) If you are using Visual Interdev add a reference to the AddEmail library: In Visual Interdev main menu, select Project -> References. Find and select AddEmail 4.0 Type Library.

 

2. Declare variable that will hold a reference to the SmtpMail object:

 

var objSmtpMail;

 

3a. Create an instance of the SmtpMail object:

 

objSmtpMail = Server.CreateObject("AddEmail.SmtpMail");

 

3b. If you are sending e-mails asynchronously you will need to access the same SmtpMail object on more than one page. In this case store an instance of the SmtpMail object in Session collection:

 

if (Session("SmtpMail") == null)

{

   objSmtpMail = Server.CreateObject("AddEmail.SmtpMail");

   Session("SmtpMail") = objSmtpMail;

}

else

{

   objSmtpMail = Session("SmtpMail");

}

 

4. Set SMTP server address, port, username and password:

 

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

objSmtpMail.SmtpPort = 25;

objSmtpMail.SmtpUsername = "jsmith";

objSmtpMail.SmtpPassword = "mypassword";

 

5a. Send an e-mail synchronously using SimpleSendScriptable or SimpleSendAttachmentScriptable:

 

var strError, numResultCode;

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

if (numResultCode == 0)

{

  // The email was sent successfully

   Response.Write("Sent successfully!");

}

else

{

  // The email was not sent, strError contains error description

   strError = objSmtpMail.GetLastErrorDescription();

   Response.Write(strError);

}

 

5b. Alternatively, create and fill MailMessage object and send it synchronously using SendScriptable or asynchronously using SendAsync:

 

var objMailMessage, objMailAttachment, objMailAddress, strError, numResultCode;

 

// 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)

{

  // The email was sent successfully

   Response.Write("Sent successfully!");

}

else

{

  // The email was not sent, strError contains error description

   strError = objSmtpMail.GetLastErrorDescription();

   Response.Write(strError);

}

 

// Alternatively, send prepared message asynchronously

// var messageNumber;

// messageNumber = objSmtpMail.SendAsync(objMailMessage, true);

// Save the message number. It is used to retrieve the status of the message

 

6. If the message was sent asynchronously use GetStatus to check the status of the message:

 

var numStatus;

numStatus = objSmtpMail.GetStatus(numMessageNumber);

switch(numStatus)

{

  case 0: //MailStatusQueued

       Response.Write("Queued");

  break;

  case 1: //MailStatusSending

       Response.Write("Sending...");

  break;

  case 2: //MailStatusSent

       Response.Write("Sent successfully!");

  break;

  case 3: //MailStatusFailed

       Response.Write("Error ");

       Response.Write(objSmtpMail.GetErrorCode(numMessageNumber));

       Response.Write("<BR>");

       Response.Write(objSmtpMail.GetErrorDescription(numMessageNumber));

  break;

  case 4: //MailStatusCanceled

       Response.Write "Message canceled";

  break;

}

 

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