You are here:

ActiveXperts.com > Email Component > How to Use > Smtp > Visual Basic .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 Visual Basic.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 Basic .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:

Visual Basic .NET

(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:

Visual Basic.NET

(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 related to the e-mail objects.

Imports AxEmail
Imports System.IO

Module SmtpProgram

  Sub Main()
    Dim objSmtpMail  As EMailMessage = New Message
    Dim objSmtp      As Smtp = New Smtp
    Dim objConstants As EMailConstants = New Constants

Step 5: Gather informaiton

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: Clear (good practise)
objSmtpMail.Clear()

' Mail: Encoding
objSmtpMail.Encoding = objConstants.EMAIL_MESSAGE_ENCODING_DEFAULT

' Mail: Priority
objSmtpMail.Priority = objConstants.EMAIL_MESSAGE_PRIORITY_MEDIUM

' Mail: From
strFromAddress = ReadInput("E-mail From:", True)
objSmtpMail.FromAddress = strFromAddress
objSmtpMail.FromName = strFromAddress ' You can assign any displayable name

' Mail: Subject
objSmtpMail.Subject = ReadInput("Subject:", False)

' Mail: Body
objSmtpMail.BodyPlainText = ReadInput("Message:", False)

' Mail: TO recipient(s)
strTo = ReadInput("E-mail to:", False)
objSmtpMail.AddTo(strTo, strTo)
Console.WriteLine("AddTo, result: " & objSmtpMail.LastError)
If (objSmtpMail.LastError <> 0) Then
  Exit Sub
End If

' Mail: CC recipient(s)
strCc = ReadInput("E-mail CC (optional):", True)
If (strCc <> "") Then
  objSmtpMail.AddCc(strCc, strCc)
  Console.WriteLine("AddCc, result: " & objSmtpMail.LastError )
  If (objSmtpMail.LastError <> 0) Then
    Exit Sub
  End If
End If

' Mail: Attachments
strAttachment = ReadInput("Attachment File (optional): ", True)
If (strAttachment <> String.Empty) Then
  objSmtpMail.AddAttachment(strAttachment)
  Console.WriteLine("AddAttachment, result: " & objSmtpMail.LastError )
  If (objSmtpMail.LastError <> 0) Then
    Exit Sub
  End If
End If

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
objSmtp.Send(objSmtpMail)
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.vb" company="ActiveXperts">
'     Copyright (c) ActiveXperts Software - www.activexperts.com
' </copyright>
' <author>ActiveXperts</author>
'-----------------------------------------------------------------------

Imports AEmailLib
Imports System.IO

Module SmtpProgram

  Sub Main()
    Dim objSmtpMail As EMailMessage = New EMailMessage
    Dim objSmtp As Smtp = New Smtp
    Dim objConstants As EMailConstants = New EMailConstants

    Dim strHost As String = String.Empty, 
        strAccount As String = String.Empty, 
        strPassword As String = String.Empty, 
        strFromAddress As String = String.Empty, 
        strTo As String = String.Empty, 
        strCc As String = String.Empty, 
        strBcc As String = String.Empty, 
        strAttachment As String = String.Empty, 
        strYN As String = String.Empty

    ' Set Logfile (optional, for debugging purposes)
    objSmtp.LogFile = String.Format("{0}{1}", 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)
    objSmtpMail.Clear()

    ' Mail: Encoding
    objSmtpMail.Encoding = objConstants.EMAIL_MESSAGE_ENCODING_DEFAULT

    ' Mail: Priority
    objSmtpMail.Priority = objConstants.EMAIL_MESSAGE_PRIORITY_MEDIUM

    ' Mail: From
    strFromAddress = ReadInput("E-mail From:", True)
    objSmtpMail.FromAddress = strFromAddress
    objSmtpMail.FromName = strFromAddress ' You can assign any displayable name

    ' Mail: Subject
    objSmtpMail.Subject = ReadInput("Subject:", False)

    ' Mail: Body
    objSmtpMail.BodyPlainText = ReadInput("Message:", False)

    ' Mail: TO recipient(s)
    strTo = ReadInput("E-mail to:", False)
    objSmtpMail.AddTo(strTo, strTo)
    Console.WriteLine("AddTo, result: " & objSmtpMail.LastError & ")")
    If (objSmtpMail.LastError <> 0) Then
        Exit Sub
    End If

    ' Mail: CC recipient(s)
    strCc = ReadInput("E-mail CC (optional):", True)
    If (strCc <> "") Then
      objSmtpMail.AddCc(strCc, strCc)
      Console.WriteLine("AddCc, result: " & objSmtpMail.LastError & ")")
      If (objSmtpMail.LastError <> 0) Then
        Exit Sub
      End If
    End If

    ' Mail: Attachments
    strAttachment = ReadInput("Attachment File (optional): ", True)
    If (strAttachment <> String.Empty) Then
      objSmtpMail.AddAttachment(strAttachment)
      Console.WriteLine("AddAttachment, result: " & objSmtpMail.LastError & ")")
      If (objSmtpMail.LastError <> 0) Then
        Exit Sub
      End If
    End If

    ' SMTP: Host
    strHost = ReadInput("SMTP server:", False)
    Do
        strYN = ReadInput("Is " & strHost & " a secure SMTP server (y/n)?", False).ToUpper()
    Loop Until (strYN.StartsWith("Y") Or strYN.StartsWith("N"))
    objSmtp.Clear()
    If (strYN.StartsWith("Y")) Then
      objSmtp.SetSecure(465)
      Console.WriteLine("SetSecure, result: " & objSmtp.LastError & ")")
      If (objSmtp.LastError <> 0) Then
        Exit Sub
      End If
    End If

    ' SMTP Account (optional)
    strAccount = ReadInput("SMTP server account (optional):", True)
    If (strAccount.Length > 0) Then
      strPassword = ReadInput("SMTP server password:", True)
    End If


    ' 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) Then
      Exit Sub
    End If

    ' SMTP: Send
    objSmtp.Send(objSmtpMail)
    Console.WriteLine("Send, result: " & objSmtp.LastError & ")")

    ' SMTP: Disconnect
    objSmtp.Disconnect()
    Console.WriteLine("Disconnected.")

    Console.WriteLine("Ready.")
    Console.WriteLine("Press <ENTER> to continue.")
    Console.ReadLine()
  End Sub

  Private Function ReadInput(ByVal strTitle, ByVal bAllowEmpty) As String

    Dim strInput As String = String.Empty
    Dim strReturn As String = String.Empty

    Console.WriteLine(strTitle)
    Do
      Console.Write("  > ")
      strInput = Console.ReadLine()

      If (strInput.Length > 0) Then
        strReturn = strInput
      End If
    Loop Until strReturn <> String.Empty Or bAllowEmpty

    ReadInput = strReturn
  End Function

End Module

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.