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


 

Retrieving email from POP3 server using VB.NET

This article will demonstrate how using the Email Factory for .NET Pop component you can retrieve emails from a POP3 server using VB.NET. 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 with events
  2. Establishing a connection
  3. Retrieving one or more Email messages
  4. Saving attachments to disk
  5. Reading message attributes
  6. Releasing a connection

Each of these processes is described in the sections below.


Creating a new Pop instance with events

Before creating a new Pop 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 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.

After you have the Jscape.Email.dll reference added to your project, you can define a Pop instance with Events.

   Dim WithEvents myPop As Pop = Nothing


Create a new Pop instance providing the POP3 server hostname, username, and password as arguments.

   myPop = New Pop("hostname", "username", "password")


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

   Public Sub OnConnected(ByVal sender As Object, ByVal e As PopConnectedEventArgs) Handles myPop.ConnectedEvent
      Console.WriteLine("Connected to {0}", e.Host)
   End Sub
Public Sub OnDisconnected(ByVal sender As Object, ByVal e As PopDisconnectedEventArgs) Handles myPop.DisconnectedEvent Console.WriteLine("Disconnected.") End Sub
Public Sub OnDataReceived(ByVal sender As Object, ByVal e As PopDataReceivedEventArgs) Handles myPop.DataReceivedEvent Console.WriteLine("Response: " + e.Response) End Sub
Public Sub OnCommandSent(ByVal sender As Object, ByVal e As PopCommandSentEventArgs) Handles myPop.CommandSentEvent Console.WriteLine("Command: " + e.Command) End Sub
Public Sub OnMessageRetrieved(ByVal sender As Object, ByVal e As PopMessageRetrievedEventArgs) Handles myPop.MessageRetrievedEvent Console.WriteLine("Command: " + e.Message.Subject) End Sub


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.

   Dim message As EmailMessage = Nothing
   Dim e As IEnumerator = myPop.GetMessages()
   While (e.MoveNext())
      message = e.Current
   End While


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
   Dim message As EmailMessage = 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.

   Dim mc As Int32 = 0
   Dim ac As Int32 = 0
   Dim e As IEnumerator = myPop.GetMessages()
   Dim message As EmailMessage = Nothing
   While (e.MoveNext())
      mc = mc + 1
      message = e.Current
      Dim a As IEnumerator = message.GetAttachments()
      While (a.MoveNext())
         ac = ac + 1
         attachment = a.Current
         ' get name of attached file
         Dim filename As String = attachment.GetFilename()
         if (filename.Length = 0) Then
            filename = "att" & mc & "_" & ac & ".txt"
         End If
         ' get data for attached file
         Dim data As Byte() = attachment.GetFileData()
         ' process the attachment
         Dim fs As New FileStream(filename, FileMode.Open, FileAccess.Write)
         Dim w As New BinaryWriter(fs)
         w.Write(data, 0, data.Length)
         fs.Close()
      End While
   End While


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.

   Dim e As IEnumerator = myPop.GetMessages()
   Dim message As EmailMessage = Nothing
   While (e.MoveNext())
      message = e.Current
      ' display message subject
      Console.Write(message.Subject & vbTab)
      ' display From address
      Console.Writeline(message.From)
      ' display message content type
      Console.Writeline(message.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