Using AddEmail in ASP.NET / C# projects

<< Click to Display Table of Contents >>

Navigation:  Samples and Tutorials > ASP.NET - C# >

Using AddEmail in ASP.NET / C# projects

 

To use AddEmail in your ASP.NET / C# project perform the following steps:

 

1. Add a reference to the AddEmail library: In Visual Studio main menu, select View -> Solution Explorer to open Solution Explorer. Right-click on the project that will use AddEmail and select Add Reference from the pop-up menu. Click COM tab, find and select AddEmail 4.0 Type Library, click Select button then OK button.

 

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

 

AddEmailLib.SmtpMailClass objSmtpMail;

 

3a. Create an instance of the SmtpMail object:

 

objSmtpMail = new AddEmailLib.SmtpMailClass();

 

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 = new AddEmailLib.SmtpMailClass();

   Session["SmtpMail"] = objSmtpMail;

}

else

{

   objSmtpMail = (AddEmailLib.SmtpMailClass)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 SimpleSend or SimpleSendAttachment:

 

string strError;

int resultCode = objSmtpMail.SimpleSend("jsmith@myserver.com", "jane@someserver.com;james@someserver.com", "test", "Test message", out strError);

if (resultCode == 0)

{

// E-mail was sent successfully

}

else

{

// Send failed, strError contains detailed error description

}

 

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

 

// Create message and setup subject and body

MailMessageClass objMessage = new AddEmailLib.MailMessageClass();

objMessage.MessageSubject = "test";

objMessage.MessageBody = "Test message";

 

// Setup sender

MailAddressClass objSender = new MailAddressClass();

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

objSender.Name = "Jane Smith";

objMessage.Sender = objSender;

 

// Setup first recipient

MailAddressClass objRecipient = new MailAddressClass();

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

objRecipient.Name = "Jane Smith";

objMessage.AddRecipient(objRecipient);

 

// Setup second recipient

objRecipient = new MailAddressClass();

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

objRecipient.Name = "James Smith";

objMessage.AddRecipient(objRecipient);

 

// Send prepared message synchronously

string strError;

int resultCode = objSmtpMail.Send(objMessage, true, out strError);

if (resultCode == 0)

{

// E-mail was sent successfully

}

else

{

// Send failed, strError contains detailed error description

}

 

// Alternatively, send prepared message asynchronously

//int messageNumber = objSmtpMail.SendAsync(objMessage, 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:

 

AddEmailLib.MailStatus messageStatus = objSmtpMail.GetStatus(messageNumber);

switch(messageStatus)

{

  case AddEmailLib.MailStatus.MailStatusQueued:

  break;

  case AddEmailLib.MailStatus.MailStatusSending:

  break;

  case AddEmailLib.MailStatus.MailStatusSent:

      // E-mail was sent successfully

  break;

  case AddEmailLib.MailStatus.MailStatusFailed:

      // E-mail wasn't sent

      int errorCode = objSmtpMail.GetErrorCode(messageNumber);

      string errorDescription = objSmtpMail.GetErrorDescription(messageNumber);

  break;

  case AddEmailLib.MailStatus.MailStatusCanceled:

  break;

}

 

 

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