using System; using System.Collections; using System.IO; using System.Text; using System.Threading; using Jscape.Email; namespace PopExample { class PopExample { public Pop myPop = null; // set default attachment folder public string attDir = "./messages/attachments/"; public PopExample(string hostname, string username, string password){ myPop = new Pop(hostname, username, password); // turn on debug mode myPop.Debug = true; // don't delete messages from server myPop.DeleteMessages = false; // 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); // test current dir structure if (!Directory.Exists(attDir)) { Directory.CreateDirectory(attDir); } // connect to pop server myPop.Connect(); int mc = 0; // retrieve all email messages IEnumerator e = myPop.GetMessages(); while(e.MoveNext()) { ++mc; int ac = 0; EmailMessage message = (EmailMessage)e.Current; // get attachments for each email, if any 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(); // save the attachment FileStream fs = new FileStream(attDir + filename, FileMode.Create, FileAccess.Write); BinaryWriter w = new BinaryWriter(fs); w.Write(data, 0, data.Length); fs.Close(); } } // your server may require a slight delay in order to respond. Thread.Sleep(100); // disconnect from server myPop.Disconnect(); } [STAThread] static void Main(string[] args) { string hostname = "mail.ourserver.com"; string username = "username@ourserver.com"; string password = "password"; PopExample popexample = new PopExample(hostname, username, password); } 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 subject= "+e.Message.Subject); } } }