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


 

Retrieving email from POP3 server using C#

This article will demonstrate how using the Email Factory for .NET Pop component you can retrieve emails from a POP3 server 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 Pop

The Email Factory for .NET Pop component provides methods for communicating with an POP3 server. Using the Email Factory for .NET components retrieving email from a POP3 or IMAP server is a very simple task. For the purposes of this article we will focus on using the POP3 protocol. Details of using the IMAP protocol to retrieve email are very much the same, the main difference being that the Pop class is used instead of the Imap class.

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

  1. Creating a new Pop instance
  2. Subscribing to Pop Events
  3. Establishing a connection
  4. Retrieving one or more Email messages
  5. Saving attachments to disk
  6. Reading message attributes
  7. Releasing a connection

Each of these processes is described in the sections below.


Creating a new Pop instance

Before creating a new Pop 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 Pop instance providing the POP3 server hostname, username, and password as arguments.

   Pop myPop = new Pop("hostname", "username", "password");


Subscribing to Pop Events

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

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

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

The following example illustrates subscribing to the Pop component events.

   // Subscribe to events
   myPop.ConnectedEvent += new Pop.ConnectedEventHandler(OnConnected);
   myPop.DisconnectedEvent += new Pop.DisconnectedEventHandler(OnDisconnected);
   myPop.DataReceivedEvent += new Pop.DataReceivedEventHandler(OnDataReceived);
   myPop.CommandSentEvent += new Pop.CommandSentEventHandler(OnCommandSent);
   myPop.MessageRetrievedEvent += new Pop.MessageRetrievedEventHandler(OnMessageRetrieved);

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

   public void OnConnected(object sender, PopConnectedEventArgs e) {
      Console.WriteLine("Connected to {0}", e.Host);
   }
   public void OnDisconnected(object sender, PopDisconnectedEventArgs e) {
      if (myPop.IsConnected()) {
         myPop.Disconnect();
      }
      Console.WriteLine("Disconnected.");
   }
   public void OnDataReceived(object sender, PopDataReceivedEventArgs e) {
      Console.WriteLine("Response: "+e.Response);
   }
   public void OnCommandSent(object sender, PopCommandSentEventArgs e) {
      Console.WriteLine("Command: "+e.Command);
   }
   public void OnMessageRetrieved(object sender, PopMessageRetrievedEventArgs e) {
      Console.WriteLine("Message:\r\n"+e.Message.GetSubject());
   }


Establishing a connection

Once a Pop instance has been created and subscribed to the Pop events you may establish a connection to the POP server by invoking the Connect() method.

   myPop.Connect();


Retrieving one or more Email messages

Upon establishing a connection to a POP server you can retrieve all messages in the Pop account mailbox by invoking the GetMessages method. Any messages found are returned as an Enumeration.

   IEnumerator e = myPop.GetMessages();
   while(e.MoveNext()) {
      EmailMessage em = (EmailMessage)e.Current;
   }


Additionally, you can retrieve a message in the account mailbox by invoking the GetMessage method and passing the one-based message ID as an argument.

   // get the first message
   EmailMessage message = myPop.GetMessage(1);


Saving attachments to disk

Once you have retrieved an email message you can then retrieve any attachments and save them to disk.

   int mc = 0;
   int ac = 0;
   IEnumerator e = myPop.GetMessages();
   while(e.MoveNext()) {
      ++mc;
      EmailMessage message = (EmailMessage)e.Current;
      // get attachments for each email
      IEnumerator ea = message.GetAttachments();
      while(ea.MoveNext()) {
         ++ac;
         Attachment a = (Attachment)ea.Current;
         // get name of attached file, if any
         String filename = a.GetFilename();
         if (filename.Length == 0) {
            // build temporary filename
            filename = "att" + mc + "_" + ac + ".txt";
         }
         // get data for attached file
         byte[] data = a.GetFileData();
         // process the attachment
         FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Write);
         BinaryWriter w = new BinaryWriter(fs);
         w.Write(data, 0, data.Length);
         fs.Close();
      }
   }


Reading message attributes

The Email Factory for .NET Pop component contains methods and properties for reading message attributes, such as the Subject, Body, Content-Type, From address, etc.

   IEnumerator e = myPop.GetMessages();
   while(e.MoveNext()) {
      EmailMessage em = (EmailMessage)e.Current;
      // display message subject
      Console.Write(em.Subject + "\t");
      // display From address
      Console.Writeline(em.From);
      // display message content type
      Console.Writeline(em.GetContentType());
   }


Releasing a connection

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

   myPop.Disconnect();


Examples

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

View example source code