Using AddEmail in C# projects
Previous  Top  Next


To use AddEmail in your 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 3.0 Type Library, click Select button then OK button.

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

private
 AddEmailLib.SmtpMailClass objSmtpMail;

3. Create an instance of the SmtpMail object in the constructor of your class:

objSmtpMail = new AddEmailLib.SmtpMailClass();

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, trueout 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);

6. If the e-mail is sent asynchronously your application will need to process events to find out when the email was sent successfully or send operation failed. To process events from the SmtpMail object declare event handlers and subscribe to events:

private void OnStatusChange(int messageNumber, int newStatus)
{
    // Event processing code

}

private void OnProgress(int messageNumber, int bytesSent, int bytesTotal)
{
    // Event processing code

}

// Subscribe to events
objSmtpMail.OnStatusChange += new
 _ISmtpMailEvents_OnStatusChangeEventHandler(OnStatusChange);
objSmtpMail.OnProgress += new
 _ISmtpMailEvents_OnProgressEventHandler(OnProgress);


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