You are here:

ActiveXperts.com > Email Component > How to Use > Smtp > Visual C#.NET
ActiveXperts Email Component

Quicklinks

ActiveXperts Email Component is a software development kit (SDK) that enables the user to send (SMTP) and receive (POP3) e-mail messages. ActiveXperts Email Component supports SMTP, POP3, multiple recipients (To, CC, BCC), multiple attachments (ASCII and binary), rich text body formats (RTF/HTML), Unicode, multiple character sets, SMTP authorization (AUTH PLAIN, AUTH LOGIN, AUTH CRAM MD5), POP3 authorization (Plain, APOP), POP3 header download, different character sets (including arabic, chinese, japanese, russian, greek, hebrew and many more), different encodings (including 7/8 bit, quoted-printable, base64).


Introduction

In this example we are going to use Visual Studio 2008 to create a console application in C#.NET project named 'DemoApp' in a solution named 'DemoSolution'. We are going to store this project in the directory 'C:\MyProjects'. All of these names can be changed according to your preferences. This demo project will ask the user to give an e-mail address and a message body on the command prompt.

Step 1: Download and install ActiveXperts Email Component

Download ActiveXperts Email Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new Visual C# .NET Project

Launch Microsoft Visual Studio from the Start menu. Choose 'New' from the 'File' menu and click on 'Project'. In the 'New Project' dialog, select a Visual Studio template. Select a name for the application and a name for the solution. Also, select the directory where you want to store the project:

VisualStudio create new project

(Click on the picture to enlarge)

Step 3: Refer to ActiveXperts Email Component Library

Now that a new project has been created, you must add a reference to ActiveXperts Email Component in the project to be able to use the the ActiveXperts Email Component objects. To do so, choose 'Add Reference...' from the 'Project' menu. In the 'Add Reference' dialog that pops up, select the 'COM' tab and select the ActiveXperts Email Component Type Library' as shown in the following picture:

VisualStudio C#.NET add reference

(Click on the picture to enlarge)

Click 'OK' to close the 'Add Reference' dialog.

Step 4: Declare and create ActiveXperts Email Component objects

The following code will show you how to declare and create the SMTP and E-mail objects. We will use the 'objSmtp' object to send the message itself. The 'objSmtpMail' object will be used to store information of the message and the 'objConstants' object containes constant values releated to the e-mail objects.

namespace SmtpProgram
{
  using System;
  using System.Collections.Generic;
  using System.Text;
  using AxEmail;
  using System.IO;

  class SmtpProgram
  {
    static void Main(string[] args)
    {
      EMailMessage objMail = new Message();
      Smtp objSmtp = new Smtp();
      EMailConstants objConstants = new Constants();

Step 5: Gather information

The following code will ask the user for the recipient e-mail address and the content of the text message. You can also specify a CC or BCC recipient and you can also add an attachment to your message. This data will be stored in the 'objEmail' object.

// Mail: From
strFromAddress = ReadInput("E-mail From:", true);
objMail.FromAddress = strFromAddress;
objMail.FromName = strFromAddress; // You can assign any displayable name

// Mail: Subject
objMail.Subject = ReadInput("Subject:", false);

// Mail: Body
objMail.BodyPlainText = ReadInput("Message:", false);

// Mail: To recipients
strTo = ReadInput("E-mail to:", false);
objMail.AddTo(strTo, strTo);
Console.WriteLine("AddTo, result: " + objMail.LastError );
if( objMail.LastError != 0 )
    return;

// Mail: Cc recipients
strCc = ReadInput("E-mail CC:", true);
objMail.AddCc(strCc, strCc);
Console.WriteLine("AddCc, result: " + objMail.LastError );
if( objMail.LastError != 0 )
  return;

// Mail: Attachment
strAttachment = ReadInput("Attachment File (optional):", true);
if (strAttachment != string.Empty)
{
  objMail.AddAttachment(strAttachment);
  Console.WriteLine("AddAttachment, result: " + objMail.LastError );
  if( objMail.LastError != 0 )
    return;
}

Step 6: Send the E-mail message

The following code shows how to send an e-mail message using the data that was stored in the 'objSmtpMail' and 'objConstants' objects.

// Smtp: Send
object obj = objMail;
objSmtp.Send(ref obj);
Console.WriteLine("Send, result: " + objSmtp.LastError);

Appendix: Full source code

Following you can find the full source code which is also included in ActiveXperts Email Component package.

//-----------------------------------------------------------------------
// <copyright file="SmtpProgram.cs" company="ActiveXperts">
//     Copyright (c) ActiveXperts Software - www.activexperts.com
// </copyright>
// <author>ActiveXperts</author>
//-----------------------------------------------------------------------

namespace SmtpProgram
{
  using System;
  using System.Collections.Generic;
  using System.Text;
  using AxEmail;
  using System.IO;

  class SmtpProgram
  {
    static void Main(string[] args)
    {
      EMailMessage objMail = new Message();
      Smtp objSmtp = new Smtp();
      EMailConstants objConstants = new Constants();

      string strHost = string.Empty, 
      strAccount     = string.Empty, 
      strPassword    = string.Empty, 
      strFromAddress = string.Empty, 
      strTo          = string.Empty, 
      strCc          = string.Empty, 
      strBcc         = string.Empty, 
      strAttachment  = string.Empty, 
      strYN          = string.Empty;

      // Set Logfile (optional, for debugging purposes)
      objSmtp.LogFile = Path.GetTempPath() + "SmtpLog.txt";

      Console.WriteLine("ActiveXperts Email Component: Build: " + objSmtp.Build + 
                                                       "; Module: " + objSmtp.Module);
      Console.WriteLine(string.Empty);

      Console.WriteLine("Log file can be found here:");
      Console.WriteLine(objSmtp.LogFile);
      Console.WriteLine(string.Empty);

      // Mail: Clear (good practise)
      objMail.Clear();

      // Mail: Encoding
      objMail.Encoding = objConstants.EMAIL_MESSAGE_ENCODING_DEFAULT;

      // Mail: Priority
      objMail.Priority = objConstants.EMAIL_MESSAGE_PRIORITY_MEDIUM;

      // Mail: From
      strFromAddress = ReadInput("E-mail From:", true);
      objMail.FromAddress = strFromAddress;
      objMail.FromName = strFromAddress; // You can assign any displayable name

      // Mail: Subject
      objMail.Subject = ReadInput("Subject:", false);

      // Mail: Body
      objMail.BodyPlainText = ReadInput("Message:", false);

      // Mail: To recipients
      strTo = ReadInput("E-mail to:", false);
      objMail.AddTo(strTo, strTo);
      Console.WriteLine("AddTo, result: " + objMail.LastError );
      if( objMail.LastError != 0 )
        return;

      // Mail: Cc recipients
      strCc = ReadInput("E-mail CC:", true);
      objMail.AddCc(strCc, strCc);
      Console.WriteLine("AddCc, result: " + objMail.LastError );
      if( objMail.LastError != 0 )
        return;

      // Mail: Attachment
      strAttachment = ReadInput("Attachment File (optional):", true);
      if (strAttachment != string.Empty)
      {
        objMail.AddAttachment(strAttachment);
        Console.WriteLine("AddAttachment, result: " + objMail.LastError );
        if( objMail.LastError != 0 )
          return;
      }

      // Smtp: Clear (good practise)
      objSmtp.Clear();

      // Smtp: Host
      strHost = ReadInput("SMTP server", false);

      // Smtp: Secure if secure communications is required
      do
      { 
        strYN = ReadInput("Is " + strHost + " a secure SMTP server (y/n)?", false).ToUpper();
      }
      while (!strYN.StartsWith("Y") && !strYN.StartsWith("N"));
      
      if (strYN.StartsWith("Y"))
      {
        objSmtp.SetSecure(465);
        Console.WriteLine("SetSecure, result: " + objSmtp.LastError );
        if( objSmtp.LastError != 0 )
          return;           
      }

      // Smtp Account/password(optional)
      strAccount = ReadInput("SMTP server account (optional)", true);
      if (strAccount.Length > 0)
          strPassword = ReadInput("SMTP server password", true);

      // Smtp: Connect
      // Pass additional Account and Password parameters if server requires authentication
      objSmtp.Connect(strHost, strAccount, strPassword);   
      Console.WriteLine("Connect, result: " + objSmtp.LastError );
      if( objSmtp.LastError != 0 )
          return;            

      // Smtp: Send
      object obj = objMail;
      objSmtp.Send(ref obj);
      Console.WriteLine("Send, result: " + objSmtp.LastError );   

      // Disconnects the SMTP server
      objSmtp.Disconnect();
      Console.WriteLine("Disconnected.");

      Console.WriteLine("Ready.");
      System.Threading.Thread.Sleep(3000); // Sleep for 3 second before exit
    }

    static private string ReadInput(string strTitle, bool bAllowEmpty)
    {
      string strInput, strReturn = string.Empty;

      Console.WriteLine(strTitle);
      do
      {
        Console.Write("  > ");
        strInput = Console.ReadLine();
        if (strInput.Length > 0)
          strReturn = strInput;
      } while (strReturn == string.Empty && !bAllowEmpty);

      return strReturn;
    }
  }
}

You can download the full source code of this project from the ActiveXperts FTP site: ftp://ftp.activexperts-labs.com/samples/smtp-pop3-component/. There are many other working samples included with the product or on the FTP site.

NOTE: Demo Projects are created with Microsoft Visual Studio 2008

The ActiveXperts Email Component project ships with a set of Microsoft Visual Studio .NET samples. The projects are created with Microsoft Visual Studio 2008.

Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.