« Communicating with a POP3 server in .NET | Main | Formatting MimeMessages using .NET »

September 14, 2008

Communicating with an IMAP4 server in .NET

Overview

       This article will demonstrate how to communicate with a IMAP4 server using Email Factory for .NET.

Code Example
      
      
Download licenseEmail.txtDownload IMAPEx.cs   |  Download IMapEx.vb

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Collections;
   4:  using System.Text;
   5:  using Jscape.Email;
   6:   
   7:  namespace IMAPNET
   8:  {
   9:      class IMAPEx
  10:      {
  11:          Imap imap = null;
  12:          public IMAPEx(string server, string username, string password)
  13:          {
  14:              imap = new Imap(server, username, password);
  15:              imap.LicenseKey = null; // copy license from license.txt
  16:              imap.DeleteMessages = true;
  17:              imap.ConnectedEvent +=
  18:                  new Imap.ConnectedEventHandler(imap_ConnectedEvent);
  19:              imap.DisconnectedEvent +=
  20:                  new Imap.DisconnectedEventHandler(imap_DisconnectedEvent);
  21:          }
  22:   
  23:          private void imap_DisconnectedEvent(object sender, ImapDisconnectedEventArgs e)
  24:          {
  25:              Console.WriteLine("Disconnected from : " + e.Host);
  26:          }
  27:   
  28:          private void imap_ConnectedEvent(object sender, ImapConnectedEventArgs e)
  29:          {
  30:              Console.WriteLine("Connected to : " + e.Host);
  31:          }
  32:   
  33:          public void openConnection()
  34:          {
  35:              imap.Connect();
  36:          }
  37:   
  38:          public void closeConnection()
  39:          {
  40:              imap.Expunge();
  41:              imap.Disconnect();
  42:          }
  43:   
  44:          public string issueCommand(string cmd)
  45:          {
  46:              return imap.IssueCommand(cmd);
  47:          }
  48:   
  49:          public int getMessageCount()
  50:          {
  51:              return imap.GetMessageCount();
  52:          }
  53:   
  54:          public ArrayList getAllMessages()
  55:          {
  56:              ArrayList ret = new ArrayList();
  57:              IEnumerator e = imap.GetMessages();
  58:              while (e.MoveNext()) ret.Add((EmailMessage)e.Current);
  59:              return ret;
  60:          }
  61:   
  62:          public ArrayList getNewMessages()
  63:          {
  64:              ArrayList ret = new ArrayList();
  65:              IEnumerator e = imap.GetNewMessages();
  66:              while (e.MoveNext()) ret.Add((EmailMessage)e.Current);
  67:              return ret;
  68:          }
  69:   
  70:          public EmailMessage getMessage(int id)
  71:          {
  72:              return imap.GetMessage(id);
  73:          }
  74:   
  75:          public void deleteMessage(int id)
  76:          {
  77:              imap.DeleteMessage(id);
  78:          }
  79:   
  80:          public ArrayList GetMessagesWithMinimumSize(long size)
  81:          {
  82:              ArrayList ret = new ArrayList();
  83:              IEnumerator e = imap.GetMessages("LARGER " + size.ToString());
  84:              while (e.MoveNext()) ret.Add((EmailMessage)e.Current);
  85:              return ret;
  86:          }
  87:   
  88:          public ArrayList GetMessagesWithMaximumSize(long size)
  89:          {
  90:              ArrayList ret = new ArrayList();
  91:              IEnumerator e = imap.GetMessages("SMALLER " + size.ToString());
  92:              while (e.MoveNext()) ret.Add((EmailMessage)e.Current);
  93:              return ret;
  94:          }
  95:   
  96:          public void setFlags(int startMessage, int endMessage,
  97:                               int mode, int flag)
  98:          {
  99:              imap.Store(startMessage, endMessage, mode, flag);
 100:          }
 101:   
 102:          static void Main(string[] args)
 103:          {
 104:              try
 105:              {
 106:                  string server = "[server]";
 107:                  string username = "[username]";
 108:                  string password = "[password]";
 109:                  IMAPEx ie = new IMAPEx(server, username, password);
 110:                  ie.openConnection();
 111:                  ie.closeConnection();
 112:              }
 113:              catch (Exception e)
 114:              {
 115:                  Console.WriteLine(e.Message);
 116:              }
 117:   
 118:              Console.WriteLine("Press any key to exit ... ");
 119:              Console.Read();
 120:          }
 121:      }
 122:  }

Lines 1-5   : Necessary import statements.
Lines 11-21 : Initialize Imap instance and connect event handlers.
Lines 23-31 : Event handlers for connect / disconnect events.
Lines 33-42 : Methods to open/close connection to the server.
Lines 44-47 : Issue a command to the server.
Lines 49-52 : Get message count.
Lines 54-60 : Get all messages from the server.
Lines 62-68 : Get new messages from the server.
Lines 70-73 : Get message with a particular id.
Lines 75-78 : Delete message with a particular id.
Lines 80-86 : Get messages with minimum size.
Lines 88-94 : Get messages with maximum size.
Lines 96-100 : Set flags for a range of messages.
Line  109   : Initialize ImapEx instance.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e550fd70338833010534a2ee75970b

Listed below are links to weblogs that reference Communicating with an IMAP4 server in .NET:

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Comments are moderated, and will not appear until the author has approved them.