« Communicating with an SMTP server and sending emails in .NET | Main | Communicating with an IMAP4 server in .NET »

September 14, 2008

Communicating with a POP3 server in .NET

Overview

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

Code Example
      
      
Download licenseEmail.txtDownload PopEx.cs  |  Download PopEx.vb

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Collections;
   4:  using System.Text;
   5:  using Jscape.Email;
   6:   
   7:  namespace POPNET
   8:  {
   9:      class PopEx
  10:      {
  11:          Pop pop = null;
  12:          public delegate void OnPopExEventHandler(string value);
  13:          public event OnPopExEventHandler OnPopEvent;
  14:   
  15:          public PopEx(string server, string username, string password)
  16:          {
  17:              pop = new Pop(server, username, password);
  18:              pop.LicenseKey = null; // copy license from license.txt here
  19:              pop.DeleteMessages = true;
  20:              pop.ConnectedEvent 
  21:                  += new Pop.ConnectedEventHandler(pop_ConnectedEvent);
  22:              pop.DisconnectedEvent
  23:                  += new Pop.DisconnectedEventHandler(pop_DisconnectedEvent);
  24:              pop.DataReceivedEvent
  25:                  += new Pop.DataReceivedEventHandler(pop_DataReceivedEvent);
  26:          }
  27:   
  28:          private void pop_DataReceivedEvent(object sender, PopDataReceivedEventArgs e)
  29:          {
  30:              OnPopEvent(e.Response);
  31:          }
  32:   
  33:          private void pop_DisconnectedEvent(object sender, PopDisconnectedEventArgs e)
  34:          {
  35:              Console.WriteLine("Disconnected from : " + e.Host);
  36:          }
  37:   
  38:          private void pop_ConnectedEvent(object sender, PopConnectedEventArgs e)
  39:          {
  40:              Console.WriteLine("Connected to : " + e.Host);
  41:          }
  42:   
  43:          public void openConnection()
  44:          {
  45:              pop.Connect();
  46:          }
  47:   
  48:          public void closeConnection()
  49:          {
  50:              pop.Disconnect();
  51:          }
  52:   
  53:          public void useAPOPAuthentication()
  54:          {
  55:              pop.AuthMode = Pop.AUTH_APOP;
  56:          }
  57:   
  58:          public string issuePopCommand(string cmd)
  59:          {
  60:              return pop.IssueCommand(cmd);
  61:          }
  62:   
  63:          public int getMessageCount()
  64:          {
  65:              return pop.GetMessageCount();
  66:          }
  67:   
  68:          public ArrayList getAllMessages()
  69:          {
  70:              ArrayList ret = new ArrayList();
  71:              IEnumerator e = pop.GetMessages();
  72:              while (e.MoveNext()) ret.Add((EmailMessage)e.Current);
  73:              return ret;
  74:          }
  75:   
  76:          public EmailMessage GetId(int id)
  77:          {
  78:              return pop.GetMessage(id);
  79:          }
  80:   
  81:          public void DeleteMessage(int id)
  82:          {
  83:              pop.DeleteMessage(id);
  84:          }
  85:   
  86:          static void Main(string[] args)
  87:          {
  88:              try
  89:              {
  90:                  string server = "[server]";
  91:                  string username = "[username]";
  92:                  string password = "[password]";
  93:                  PopEx pe = new PopEx(server, username, password);
  94:                  pe.useAPOPAuthentication();
  95:                  pe.openConnection();
  96:                  pe.issuePopCommand("HELO 10.0.0.1");
  97:                  pe.closeConnection();
  98:              }
  99:              catch (Exception e)
 100:              {
 101:                  Console.WriteLine(e.Message);
 102:              }
 103:   
 104:              Console.WriteLine("Press any key to exit ... ");
 105:              Console.Read();
 106:          }
 107:      }
 108:  }

Lines 1-5   : Necessary import statements.
Lines 11-26 : Initialize Pop instance and connect event handlers.
Lines 28-36 : Methods to catch events from Pop instance.
Lines 43-51 : Methods to connect/disconnect from server.
Lines 53-56 : Use APOP authentication.
Lines 58-61 : Issue a command to the server.
Lines 68-74 : Get all messages from the server.
Lines 76-79 : Get message by id.
Lines 81-84 : Delete message by id.
Line  93    : Initialize PopEx instance.

TrackBack

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

Listed below are links to weblogs that reference Communicating with a POP3 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.