Imports System Imports System.IO Imports System.Text Imports System.Threading Imports Jscape.Email Module Module1 Public Class PopExample Public WithEvents myPop As Pop = Nothing Public message As EmailMessage = Nothing ' set default attachment folder Public attDir As String = "./messages/attachments/" Public Sub New(ByVal hostname As String, ByVal username As String, ByVal password As String) myPop = New Pop(hostname, username, password) ' turn on debug mode myPop.Debug = True ' don't delete messages from server myPop.DeleteMessages = False ' test current dir structure If (Directory.Exists(attDir) = False) Then Directory.CreateDirectory(attDir) End If ' connect to pop server myPop.Connect() Dim mc As Int32 = 0 ' retrieve all email messages Dim e As IEnumerator = myPop.GetMessages() While (e.MoveNext()) mc = mc + 1 Dim ac As Int32 = 0 Dim message As EmailMessage = e.Current ' get attachments for each email, if any Dim ea As IEnumerator = message.GetAttachments() While (ea.MoveNext()) ac = ac + 1 Dim a As Attachment = ea.Current ' get name of attached file, if any Dim filename As String = a.GetFilename() If (filename.Length = 0) Then ' build temporary filename filename = "att" & mc & "_" & ac & ".txt" End If ' get data for attached file Dim data As Byte() = a.GetFileData() ' save the attachment Dim fs As FileStream = New FileStream(attDir & filename, FileMode.Create, FileAccess.Write) Dim w As BinaryWriter = New BinaryWriter(fs) w.Write(data, 0, data.Length) fs.Close() End While End While ' your server may require a slight delay in order to respond. Thread.Sleep(100) ' Disconnect from Pop server myPop.Disconnect() End Sub Public Shared Sub Main() Dim hostname As String = "mail.ourserver.com" Dim username As String = "username@ourserver.com" Dim password As String = "password" Dim popex As PopExample = New PopExample(hostname, username, password) End Sub 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("Message subject= " & e.Message.Subject) End Sub End Class End Module