Imap .NET Component
The Imap .NET Component provides a simple interface for communicating with an IMAP4 (Internet Message Access Protocol v4) 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 IMAP 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 an IMAP4 server using the Imap .NET component.
/*
* ImapExample.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 ImapExample {
class ImapExample {
public Imap myImap = null;
// set default attachment folder
public string attDir = "./messages/attachments/";
public ImapExample(string hostname, string username, string password){
myImap = new Imap(hostname, username, password);
// turn on debug mode
myImap.Debug = true;
// don't delete messages from server
myImap.DeleteMessages = false;
// Subscribe to events
myImap.ConnectedEvent += new Imap.ConnectedEventHandler(OnConnected);
myImap.DisconnectedEvent += new Imap.DisconnectedEventHandler(OnDisconnected);
myImap.DataReceivedEvent += new Imap.DataReceivedEventHandler(OnDataReceived);
myImap.CommandSentEvent += new Imap.CommandSentEventHandler(OnCommandSent);
myImap.MessageRetrievedEvent += new Imap.MessageRetrievedEventHandler(OnMessageRetrieved);
// test current dir structure
if (!Directory.Exists(attDir)) {
Directory.CreateDirectory(attDir);
}
// connect to Imap server
myImap.Connect();
int mc = 0;
// retrieve all email messages
IEnumerator e = myImap.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
myImap.Disconnect();
}
[STAThread]
static void Main(string[] args) {
string hostname = "mail.ourserver.com";
string username = "username@ourserver.com";
string password = "password";
ImapExample imapexample = new ImapExample(hostname, username, password);
}
public void OnConnected(object sender, ImapConnectedEventArgs e) {
Console.WriteLine("Connected to {0}", e.Host);
}
public void OnDisconnected(object sender, ImapDisconnectedEventArgs e) {
if (myImap.IsConnected()) {
myImap.Disconnect();
}
Console.WriteLine("Disconnected.");
}
public void OnDataReceived(object sender, ImapDataReceivedEventArgs e) {
Console.WriteLine("Response: "+e.Response);
}
public void OnCommandSent(object sender, ImapCommandSentEventArgs e) {
Console.WriteLine("Command: "+e.Command);
}
public void OnMessageRetrieved(object sender, ImapMessageRetrievedEventArgs e) {
// Uncomment the following line to display each message subject
//Console.WriteLine("Message subject= "+e.Message.Subject);
}
}
}
 
|
|