Read and Delete mails through POP3 using C#

Asked By 0 points N/A Posted on -
qa-featured

Hello everyone.

I need to delete mails of my inbox through asp.net. Is there any way to perform this concept?

I have heard something like an open-source library called OpenPop.NET.

Below is a screen shot of the program I managed to search online, but I'm still having a hard time understanding it.

Waiting for your responses.

Thanks you.

Check Mail

Do you want to delete this message?

yes No

SHARE
Answered By 0 points N/A #136462

Read and Delete mails through POP3 using C#

qa-featured

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…………….

Related Questions