Read and Delete mails through POP3 using C#
For storing the mail details (like Subject, message number, etc.), I created a custom class as below:
  1:    usingSystem;
Â
  2:    usingSystem.Collections.Generic;
  3:   usingSystem.Text;
  4:
  5:    namespacePOP3_MailHandling
  6:    {
  7:    /// <summary>
  8:    /// Custom class that holds the Mail details
  9:    /// </summary>
 10:    publicclassPop3Mails
 11:    {
 12:        /// <summary>
 13:        /// Message number of the mail.
 14:        /// This number is used to delete the mails
 15:        /// </summary>
 16:        publicintMessageNumber { get; set; }
 17:
 18:        /// <summary>
 19:        /// Subject of the message
 20:        /// </summary>
 21:        publicstringSubject { get; set; }
 22:    }
 23:}
This class is populated with the mail details into a Generic List collection, which is then bound to a ListBox to display the details.
The code for loading messages and deleting the selected mail is as below and is commented well, so that comes to be self-explanatory.
  1:   usingSystem;
  2:   usingSystem.Collections.Generic;
  3:   usingSystem.ComponentModel;
  4:        usingSystem.Data;
  5:        usingSystem.Drawing;
  6:        usingSystem.Text;
  7:        usingSystem.Windows.Forms;
  8: Â
  9:       // POP.NET Namespace
 10:      usingOpenPop.Mime;
 11:       usingOpenPop.Mime.Header;
 12:       usingOpenPop.Pop3;
 13:      usingOpenPop.Pop3.Exceptions;
 14:      usingOpenPop.Common.Logging;
 15: Â
 16:      // Used to avaoid ambiguity between
 17:      // System.Windows.Forms.Message & OpenPop.Mime.Message
 18:      usingPop3Message = OpenPop.Mime.Message;
 19: Â
 20:      namespacePOP3_MailHandling
 21:      {
 22:      publicpartialclassForm1 : Form
 23:     {
 24:        Pop3Client _pop3;      Â
 25:        Pop3Message _message;
 26:       Â
 27:        Pop3Mails _mails;
 28:        List<Pop3Mails> _lstMails;
 29: Â
 30:        /// <summary>
 31:        /// Gets the total Message Count
 32:        /// </summary>
 33:        int_totalMsgCount = 0;
 34:       Â
 35:        publicForm1()
 36:        {
 37:            InitializeComponent();
 38:        }
 39: Â
 40:        /// <summary>
 41:        /// Check Mail
 42:        /// </summary>
 43:        /// <param name="sender"></param>
 44:        /// <param name="e"></param>
 45:        privatevoidbtnCheckMail_Click(objectsender, EventArgs e)
 46:        {
 47:            _pop3 = newPop3Client();
 48:           Â
 49:            try
 50:            {
 51:                // Connect to mail-server
 52:                _pop3.Connect(txtPop3Server.Text.Trim(), Convert.ToInt32(txtPort.Text.Trim()), chkSSL.Checked);
 53: Â
 54:                if(_pop3.Connected)
 55:                {
 56:                    // Authenticate
 57:                    _pop3.Authenticate(txtUsername.Text.Trim(), txtPassword.Text.Trim());
 58: Â
 59:                    // Get Total Count
 60:                    _totalMsgCount = _pop3.GetMessageCount();
 61:                    lblTotalMsg.Text = _totalMsgCount.ToString();
 62: Â
 63:                    // Check for Mails
 64:                    if(_totalMsgCount == 0)
 65:                    {
 66:                        MessageBox.Show("Mailbox is empty");
 67:                        return;
 68:                    }
 69:                   Â
 70:                    // Clear Listbox                  Â
 71:                    lstboxMails.Items.Clear();
 72: Â
 73:                    _lstMails = newList<Pop3Mails>();
 74:                   Â
 75:                    // Loop through Mails
 76:                    for(into = _totalMsgCount; i <= _totalMsgCount; i–)
 77:                    {
 78:                        if(i == 0)
 79:                            break;
 80: Â
 81:                        // Allow to handle other messages in the message queue
 82:                        // Thus preventing the application to show 'Not Responding'
 83:                        Application.DoEvents();
 84: Â
 85:                        // Mail
 86:                        _message = _pop3.GetMessage(i);
 87: Â
 88:                        // Get mail details
 89:                        _mails = newPop3Mails();
 90:                        _mails.MessageNumber = i;
 91:                        _mails.Subject = _message.Headers.Subject;
 92: Â
 93:                        _lstMails.Add(_mails);
 94:                    }
 95: Â
 96:                    // Bind Mail details
 97:                    lstboxMails.DisplayMember = "Subject";
 98:                    lstboxMails.ValueMember = "MessageNumber";
 99:                    lstboxMails.DataSource = _lstMails;
100: Â
 101:                    MessageBox.Show("Mail fetching Completed!");
102:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
103:Â Â Â Â Â Â Â Â Â Â Â Â }
104:Â Â Â Â Â Â Â Â Â Â Â Â catch(Exception exp)
105:Â Â Â Â Â Â Â Â Â Â Â Â {
106:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â MessageBox.Show("Oops! An error occured: "+ exp.Message);
107:Â Â Â Â Â Â Â Â Â Â Â Â }
108:Â Â Â Â Â Â Â Â }
109: Â
 110:        /// <summary>
111:Â Â Â Â Â Â Â Â /// Delete Mail
112:Â Â Â Â Â Â Â Â /// </summary>
113:Â Â Â Â Â Â Â Â /// <param name="sender"></param>
114:Â Â Â Â Â Â Â Â /// <param name="e"></param>
115:Â Â Â Â Â Â Â Â privatevoidbtnDeleteMail_Click(objectsender, EventArgs e)
116:Â Â Â Â Â Â Â Â {
117:Â Â Â Â Â Â Â Â Â Â Â Â DialogResult dlgResult = MessageBox.Show("Do you want to delete this message?", "Confirmation", MessageBoxButtons.YesNo);
118: Â
 119:            if(dlgResult == System.Windows.Forms.DialogResult.No)
120:Â Â Â Â Â Â Â Â Â Â Â Â {
121:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return;
122:Â Â Â Â Â Â Â Â Â Â Â Â }
123: Â
 124:            // Get the selected mail item
125:Â Â Â Â Â Â Â Â Â Â Â Â Pop3Mails _mailSel = lstboxMails.SelectedItem asPop3Mails;
126: Â
 127:            if(_mailSel != null)
128:Â Â Â Â Â Â Â Â Â Â Â Â {
129:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Set the message count to be deleted
130:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â _pop3.DeleteMessage(_mailSel.MessageNumber);
131: Â
 132:                // Calling disconnected deletes the mail from server
133:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â _pop3.Disconnect();
134:Â Â Â Â Â Â Â Â Â Â Â Â }
135:Â Â Â Â Â Â Â Â Â Â Â Â else
136:Â Â Â Â Â Â Â Â Â Â Â Â {
137:Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â MessageBox.Show("Please select a mail to delete");
138:Â Â Â Â Â Â Â Â Â Â Â Â }
139:Â Â Â Â Â Â Â Â }
140:Â Â Â Â }
141:}
For more Download the attached file…………….