Java and .NET components, FTP, TELNET, SMTP, POP3, IMAP, HTTP, SSH
Home Search Knowledge Base Support

Support

Click here for access to live sales support.

For technical support please submit a ticket to the Help Desk.

 

Java and .NET Help

iTunes Gift Card Offer

For a limited time get a $50.00 iTunes gift card free with qualifying purchase.

Click for details.


News

JSCAPE Secure FTP Server 7.0 Released
08/20/2010 03:31 AM

JSCAPE launches Employee Giving Program
07/03/2010 08:26 AM

SSH Factory 3.6 Released
07/03/2010 06:22 AM

Secure iNet Factory 8.5 Released
07/03/2010 06:07 AM

Secure FTP Factory 8.5 Released
07/03/2010 06:03 AM

Secure FTP Applet 6.2 Released
06/10/2010 02:23 PM

more...


Tutorials

Enabling Phone Authentication
04/08/2009 11:24 AM

Detecting and Handling Brute Force Password Attacks
01/29/2009 09:44 AM

Creating a Domain
12/15/2008 11:33 AM

Public key authentication with SFTP
10/02/2008 07:59 AM

Formatting MimeMessages using .NET
09/14/2008 04:31 PM

Communicating with an IMAP4 server in .NET
09/14/2008 03:54 PM

more...


Articles

Open up corporate data to your partners
08/03/2010 10:01 PM

Access vital corporate documents on the go
07/01/2010 02:15 PM

SFTP and Encryption
05/17/2010 09:52 PM

Streamlining web uploads with ZIP archives
12/14/2009 10:11 AM

Using regular expressions in complex trigger conditions
09/08/2009 07:42 AM

Using custom forms to automate business processes
07/03/2009 08:51 AM

more...


Feedback

Request a feature or component

Request a Java or .NET component


 

Sending Email using VB.NET

This article will demonstrate how using the Email Factory for .NET SMTP component you can create and send emails using VB.NET. In addition to support for SMTP, Email Factory for .NET also includes components for POP3, IMAP, POP3SSL, IMAPSSL and MIME.

smtp .net vb.net emailsmtp .net vb.net email

Example

The source code for this article is available for download and for viewing.

View example source code

Overview of SMTP

The Email Factory for .NET SMTP component provides methods for communicating with an SMTP server. The process for establishing an interactive session with an SMTP server using the Email Factory for .NET component is as follows:

  1. Creating a new Smtp instance with events
  2. Establishing a connection
  3. Creating an email message
  4. Defining addressing and other message properties
  5. Defining the message body
  6. Adding message attachments
  7. Sending an email message
  8. Releasing a connection

Each of these processes is described in the sections below.

Creating a new Smtp instance with events

Before creating a new Smtp instance, ensure that the Jscape.Email scope is defined in your Imports statements, and that the Jscape.Email.dll is referenced in your project. Refer to Getting Started in the Email Factory for .NET Help for more information about adding the Jscape.Email.dll reference to your projects.

The Smtp component may publish one or more events during the lifetime of an SMTP session. Any object that subscribes to events published by the Smtp component can receive and process the following events.

  • The ConnectedEvent is fired by the Smtp instance once a connection to the SMTP server has been established.
  • The DisconnectedEvent is fired by the Smtp instance once the connection to the SMTP server has been released.
  • The CommandSentEvent is fired by the Smtp instance for each command sent to the SMTP server.
  • The DataReceivedEvent is fired by the Smtp instance for each response received from the SMTP server.

After you have the Jscape.Email.dll reference added to your project, you can define an Smtp instance with events.

Dim WithEvents mySmtp As Smtp = Nothing

To create a new Smtp instance, provide the SMTP server hostname as an argument.

mySmtp = New Smtp("hostname")

Now you can create the event handler methods for the Smtp events.

Public Sub OnConnected(ByVal sender As Object, ByVal e As SmtpConnectedEventArgs) Handles mySmtp.ConnectedEvent

Console.WriteLine("Connected to {0}", e.Host) End Sub

Public Sub OnDisconnected(ByVal sender As Object, ByVal e As SmtpDisconnectedEventArgs) Handles mySmtp.DisconnectedEvent

Console.WriteLine("Disconnected.") End Sub

Public Sub OnDataReceived(ByVal sender As Object, ByVal e As SmtpDataReceivedEventArgs) Handles mySmtp.DataReceivedEvent

Console.WriteLine("Response: " + e.Response) End Sub

Public Sub OnCommandSent(ByVal sender As Object, ByVal e As SmtpCommandSentEventArgs) Handles mySmtp.CommandSentEvent

Console.WriteLine("Command: " + e.Command) End Sub

Establishing a connection

Once an Smtp instance has been created and the Smtp event handlers defined, you may establish a connection to the SMTP server by invoking the Connect() method.

mySmtp.Connect()

Creating an Email message

To create an email message, construct a new EmailMessage instance passing the To: address and From: address to the constructor. You may also assign the To and From properties on the EmailMessage instance if you are using the default US-ASCII character set encoding. See the following section for more information about specifying message properties. If you are using another character set encoding, use the SetTo and SetFrom methods. Refer to the Email Factory for .NET Help for more information.

Public message As EmailMessage = Nothing
' create instance with To and From addresses
message = New EmailMessage("ToAddress", "FromAddress")

Defining addressing and other message properties

In addition to passing the To and From addresses to the EmailMessage constructor, you can specify the properties on the message instance. The EmailMessage class also has properties and methods for adding carbon-copy, blind-carbon-copy, and reply-to recipients, and message subject and priority.

Note: The To and From addresses are required properties.

message = New EmailMessage
message.To = "ToAddress"
message.From = "FromAddress"
message.Cc = "CcAddress"
message.Bcc = "BccAddress"
message.ReplyTo = "ReplyToAddress"
message.Priority = "normal"
message.Subject = "This is a test subject"

Defining the message body

You can set the message body to the contents of a file, a database query result, or a hand-coded message, as shown in the following example. Refer to the Email Factory for .NET Help for more information about defining the message body contents from a FileStream.

message.SetBody("This is a sample message")

Adding message attachments

You can add one or more attachments to an email message by invoking the AddAttachment() method passing an Attachment instance as an argument. The attachment instance can be constructed by passing a file path as an argument.

Note: Due to the way that the message body is copied when adding attachments it is important that attachments be added only after the body of the message has been defined. Changing the Body, Content-Type, or Content-Transfer-Encoding of the message after attachments have been added may result in undesired effects.

Public att As Attachment = Nothing
att = New Attachment("c:\home\report.doc")
message.AddAttachment(att)

Sending an Email message

To send a message through an established connection simply invoke the Send() method passing the message instance as an argument.

mySmtp.Send(message)

Releasing a connection

To release an established connection simply invoke the Disconnect() method as follows:

mySmtp.Disconnect()

Example

The source code for this article is available for download and for viewing.

View example source code