Pop .NET Component
The Pop .NET Component provides a simple interface for communicating with a POP (Post Office Protocol v3) server.
 
Features
- 100% managed C# - A highly scalable solution with no dependencies on 3rd party libraries.
- Email address helper classes for parsing email addresses.
- Royalty-Free Distribution - No runtime fees!
- Easily retrieve attachments from email messages.
- Serialization support allows you to easily store email messages to disk or a database
- International support for multiple character sets.
- Thread safe - Critical code blocks are synchronized for use by multiple threads.
- Event Model - Multiple events for capturing POP activity
- Integrated Help - Automatic integration of Help 2.0 documentation for Visual Studio .NET
Code Example
The following C Sharp example demonstrates retrieving messages from a POP3 server using the Pop .NET component.
/*
* PopExample.cs
*
* Copyright (c) 1999-2005 JSCAPE, LLC
* 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information of
* JSCAPE ("Confidential Information"). You shall not disclose such
* Confidential Information and shall use it only in accordance with
* the terms of the license agreement you entered into with JSCAPE.
*/
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) {
// Uncomment the following line to display each message subject
//Console.WriteLine("Message subject= "+e.Message.Subject);
}
}
}
 
|
|