After Deleting Email... Reading Next Email Leads to an issue

May 15, 2014 at 4:03 PM
Edited May 15, 2014 at 4:36 PM
Hi All,

I am getting an issue .... And the Issue is After deleting email from the inbox and when i start to read the next email it leads to an issue. I am using Pop3 Protocol. I need the solution ASAP. Please look into this issue
using (Pop3Client objPop3Client = new Pop3Client(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["SmptServerName"])))
            {
                objPop3Client.ServerName = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["SmptServerName"]);
                objPop3Client.Port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["POP3Port"]);
                objPop3Client.AuthenticateMode = Pop3AuthenticateMode.Pop;
                objPop3Client.Ssl = true;

                objPop3Client.UserName = "XXX";
                objPop3Client.Password = "XXX";

                // Authenticating the Username and Password which is stored on the server

                if (objPop3Client.Authenticate())
                {
                    // Downloading all the emails and getting the actual email count

                    Int32 totalMailCount = Convert.ToInt32(objPop3Client.GetTotalMessageCount());

                    for (int j = 1; j <= totalMailCount; j++) // Looping through emails 
                    {
                        MailMessage objMailMessage = objPop3Client.GetMessage(j); // Getting the Current Message
                        bool bDeleteFlag = objPop3Client.DeleteMail((long)objMailMessage.Index); 
                    }
                }
            }
An unhandled exception of type 'HigLabo.Net.Mail.MailClientException'

Here is the actual image of error page

Image
Image
Coordinator
May 16, 2014 at 4:12 AM
Hi.
Please read it.Especially Delete mail section.
http://www.codeproject.com/Articles/404066/Understanding-the-insides-of-the-POP-mail-protoco
This article explain how server and client communicate each other.
You can solve such problem if you get more understanding about the inside of protocol.

And use overload version of DeleteMail method.
public Boolean DeleteMail(params Int64[] mailIndex)

It may solve your problem.
I'll help you ASAP if I can, but sometimes I could not because of my other work.


regards.
May 16, 2014 at 6:24 AM
Edited May 16, 2014 at 6:25 AM
Thanks for your reply... Higty... I am using DeleteMail Method only to delete the mails as you can see in my code.

Now You have told me to read the documentation of your deleteMail method I have gone through that but will you please explain what is the problem is it my code which is creating an issue or something different. At the end of the day below is my requirement which i need to full fill. Say me whether I can read the emails or not after deleting the email or not ? Or can you give me some workaround or not ?

What is my requirement : I will start reading the emails and if email doesn't contain any attachment then i will delete that email and loop to the next email now the problem is when i execute the DeleteMail Method it successfully deletes the email & when it tries to read the next email its crashes.

Below is your explanation related to Delete Email functionality.

Here is the delete process in POP3:

Image

You can see that the response text of the DELE command is like below.

Collapse | Copy Code
+OK marked for deletion
The Dele command marks the mail that you indicate as candidate for deletion. These marked mails will be deleted when you send the quit command. You can delete a mail by using the DeleteMail method of the Pop3Client object.

Collapse | Copy Code
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage mg = null;
String htmlText = "";

using (Pop3Client cl = new Pop3Client("pop.gmail.com"))
{
    cl.Port = 995;
    cl.UserName = "xxxxx";
    cl.Password = "yyyyy";
    cl.Ssl = true;
    cl.AuthenticateMode = Pop3AuthenticateMode.Auto;

    cl.DeleteMail(1, 2, 3);
}
}
There is no authenticate required since all processes (open connection, authenticate, dele, quit) will automatically execute inside the DeleteMail method. Here is an implementation of the DeleteMail method:

Collapse | Copy Code
public Boolean DeleteMail(params Int64[] mailIndex)
{
DeleCommand cm = null;
String s = "";

if (this.EnsureOpen() == Pop3ConnectionState.Disconnected) { return false; }
if (this.Authenticate() == false) { return false; }
for (int i = 0; i < mailIndex.Length; i++)
{
    cm = new DeleCommand(mailIndex[i]);
    s = this.Execute(cm);
    if (MailParser.IsResponseOk(s) == false) { return false; }
}
this.ExecuteQuit();
return true;
}
You can see the open connection, authenticate, send dele command, and finally send quit command inside of the DeleteMail method.
Coordinator
May 16, 2014 at 7:38 AM
Hi.

I said
And use overload version of DeleteMail method.
public Boolean DeleteMail(params Int64[] mailIndex)
It seems that you correct mailIndex list of which the message does not have attachment, and delete mail by DeleteMail(params long[] mailIndexList) method.
But I don't know the requirement what you are said by manager exactly, so I may say something wrong...
I think that it would be better that you decide these decision by yourself than I determine because I don't know the detail of requirement.
So, I show you article url that may help you to understand about Pop3 protocol.


regards.
May 16, 2014 at 8:31 AM
Edited May 16, 2014 at 8:34 AM
Higty thanks again for your feedback... Sorry if my words were harsh in my previous post but I am trying my best to explain you my functionality. Actually I need a little bit of help from u.

Let me explain you again from scratch and you suggest me possible workaround for my issue : - My Program will start reading the emails from Inbox folder and Suppose If an email doesn't contain any attachment then it will directly delete the email from my inbox and then I loop back again to read the next email.

Now the issue is : - When I delete's the unwanted email (the very first email) then the program throws an exception : An unhandled exception of type 'HigLabo.Net.Mail.MailClientException'

Now I gone through your documentation but I am little confused. I am successful in deleting email but when I read the next email its crashes so how can i read the next email without getting my application to crash ?
Coordinator
May 16, 2014 at 10:03 AM
Hi.

DeleteMail method send quit command, so you must reauthenticate to server.
Please try to create new instance and authenticate and receive mail.


regards..
Marked as answer by rickjackson on 5/16/2014 at 4:24 AM
May 16, 2014 at 12:24 PM
Thanks Higty I solved my issue with your help... Thanks Again
Coordinator
May 16, 2014 at 1:18 PM
Hi.
Good news!!