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

iPod shuffle offer

For a limited time get the newly re-designed iPod shuffle free with qualifying purchase.

Click for details.


News

Secure iNet Factory 8.0 Released
04/04/2008 02:24 PM

Updates to Email Factory for .NET and Secure iNet Factory
03/29/2008 04:06 PM

JSCAPE Secure FTP Server 3.9 Preview
03/14/2008 12:19 PM

AnyClient Service and Application Launched
03/12/2008 03:41 PM

JSCAPE Secure FTP Server 3.8 Released
02/12/2008 10:50 AM


Tutorials

Email Validation with Java
04/15/2008 02:04 PM

Sending HTML Based Email Using Java
03/11/2008 02:47 PM

Secure FTP Using Java and FTPS (FTP over SSL)
03/10/2008 04:08 PM

FTP Directory Listing Using Java
03/10/2008 03:57 PM

Sending Email Using Java
03/09/2008 03:43 PM

SSH Using Java
03/09/2008 02:53 PM


Articles

DMZ File Transfer Streaming
03/28/2008 11:57 AM

Phishing looks to FTP to distribute malware
03/13/2008 05:14 PM

Ad Hoc File Transfer Explained
03/13/2008 09:16 AM

Password Policies Made Easy
03/12/2008 03:03 PM


Feedback

Request a feature or component

Request a Java or .NET component


 

Sending Email using C#

This article will demonstrate how using the Email Factory for .NET Smtp component you can create and send emails using C#. To see what else Email Factory for .NET has to offer Download a FREE 30 day Email Factory for .NET Evaluation.


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
  2. Subscribing to Smtp Events
  3. Establishing a connection
  4. Creating an Email message
  5. Defining addressing and other message properties
  6. Defining the message body
  7. Adding message attachments
  8. Sending an Email message
  9. Releasing a connection

Each of these processes is described in the sections below.


Creating a new Smtp instance

Before creating a new Smtp instance, ensure that the Jscape.Email scope is defined in your using 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.

Create a new Smtp instance providing the SMTP server hostname as an argument.

   Smtp mySmtp = new Smtp("hostname");


Subscribing to Smtp Events

The Smtp instance MUST subscribe to the Smtp events prior to invoking the Connect() method to ensure that all data sent by the SMTP server is captured.

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.

The following example illustrates subscribing to the Smtp component events.

   // Subscribe to events
   mySmtp.ConnectedEvent += new Smtp.ConnectedEventHandler(OnConnected);
   mySmtp.DisconnectedEvent += new Smtp.DisconnectedEventHandler(OnDisconnected);
   mySmtp.DataReceivedEvent += new Smtp.DataReceivedEventHandler(OnDataReceived);
   mySmtp.CommandSentEvent += new Smtp.CommandSentEventHandler(OnCommandSent);

Now you can create the event handler methods you previously defined when subscribing to the Smtp events.

   public void OnConnected(object sender, SmtpConnectedEventArgs e) {
      Console.WriteLine("Connected to {0}", e.Host);
   }
   public void OnDisconnected(object sender, SmtpDisconnectedEventArgs e) {
      if (mySmtp.IsConnected()) {
         mySmtp.Disconnect();
      }
      Console.WriteLine("Disconnected.");
   }
   public void OnDataReceived(object sender, SmtpDataReceivedEventArgs e) {
      Console.WriteLine("Response: "+e.Response);
   }
   public void OnCommandSent(object sender, SmtpCommandSentEventArgs e) {
      Console.WriteLine("Command: "+e.Command);
   }


Establishing a connection

Once an Smtp instance has been created and subscribed to the Smtp events 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.

   EmailMessage 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.

   EmailMessage 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.

   Attachment 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();


Examples

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

View example source code