Manage single cancellation with Pop3

Hello,

I am using POP3Client to manage my emails.

In my function readEmailPop3, there's a connection to the server and all the stuff I need to properly connect and then fetch messages.

The problem I noticed is the following: with Aspose POP3Client I cannot delete a single message, but I have to delete ALL messages from the client (with DeleteAllMessages()) and then commit the deletion (CommitDeletes()). When I fetch a big amount of messages (and after the fetch I can also download and save many attachments - it takes a long time!), I cannot use the DeleteAllMessages because in the meanwhile it could happen new messages to come.

Do you think there will be a release where Aspose POP3Client can manage SINGLE messages instead of ALL message in purpose of deletion? If not, how can I solve my issue?

Thank you very much for your precious time and help.

Andrea

Hi Andrea,

Thank you for writing to us.

The Pop3Client’s DeleteMessage(SequenceNumber) method allows to mark a single message for deletion. Please give a try to this method (as shown in the following code sample) and let us know your feedback.

Sample Code:

// create a pop3 client
Aspose.Email.Pop3.Pop3Client client;
client = new Aspose.Email.Pop3.Pop3Client();

// basic settings (required)
client.Host = "exchange.domain.com";
client.Username = "username";
client.Password = "password";
client.Port = 995; // set SSL port
client.SecurityMode = Aspose.Email.Pop3.Pop3SslSecurityMode.Implicit; // set implicit security mode
client.EnableSsl = true; // enable SSL
client.Connect(); // establish a connection
client.Login(); // login

Pop3MessageInfoCollection msgColl = client.ListMessages();
Console.WriteLine("Total Messages: " + msgColl.Count);

foreach (Pop3MessageInfo msgInfo in msgColl)
{
    Console.WriteLine("Message Subject: " + msgInfo.Subject);
    //delete based on some criteria and commit for single delete
    client.DeleteMessage(msgInfo.SequenceNumber);
    client.CommitDeletes();
    break;
}

Hello Kashif,

thanks for your quick response.
Unluckily I cannot use Pop3MessageInfoCollection to iterate my list of messages because I have to save the mail. So, I iterate through the messages using client.FetchMessage(i): the MailMessage object allow me to SAVE the message. Pop3MessageInfo doesn’t allow me to save it.

So I compared MailMessage and Pop3MessageInfo. There’s no way to match each other: in Pop3MessageInfo there’s an attribute named Identifier (same value as UniqueId) but there’s no way to match it with any attributes of the MailMessage object.

I also tried (but with no success) to use DeleteMessage in MailMessage with MessageId value but it’s not allowed (it is possible when you use ImapClient).

Hope it’s clear. How can I solve this issue?

Thank you,

Andrea

Hi Andreas,

I have analyzed your requirements and I think a little modification in my earlier code sample can fulfill your needs. Using Pop3MessageInfo, you have the information of message’s sequence number that can be used to fetch and then save the message to disc prior to deleting. Please have a look at the following code sample and let us know your feedback.

Sample Code:

Pop3MessageInfoCollection msgColl = client.ListMessages();
Console.WriteLine("Total Messages: " + msgColl.Count);

foreach (Pop3MessageInfo msgInfo in msgColl)
{
    Console.WriteLine("Message Subject: " + msgInfo.Subject);
    MailMessage msg = client.FetchMessage(msgInfo.SequenceNumber);
    msg.Save(msg.Subject + ".msg", MailMessageSaveType.OutlookMessageFormatUnicode);
    client.DeleteMessage(msgInfo.SequenceNumber);
    client.CommitDeletes();
    break;
}
Hello Kashif,
I am sorry, but this solution doesn't work.
If I delete a message inside the For it seems I "steal" a sequenceNumber and exception is thrown.
If you test the code you provided me with (for example) 2 messages in Inbox, you will correctly save the first email, but when fetching the second one an exception is thrown (Invalid message number).
Do you have any other ideas?

Thank you.

Andrea

Hi Kashif,

I solved it, I iterate the list from the bottom.
Thanks

Andrea

Hi Andreas,

Thank you for providing feedback.

The method was suggested based on your requirements of avoiding DeleteAllMessages. For your requirements, you can play with the logic to avoid the exception that you have mentioned. For example, you don’t want to use DeleteAllMessages( ) but want to delete messages with specific sequence numbers, you can move the CommitDeletes() statement outside the loop and it should work for you. I have tested it with 4 messages from my inbox and it worked fine at my end. Please give a try to the following code sample and share your feedback with us.

Sample Code:

Pop3MessageInfoCollection msgColl = client.ListMessages();

foreach (Pop3MessageInfo msgInfo in msgColl)
{
    Console.WriteLine("Marking for deletion: " + msgInfo.Subject);
    client.DeleteMessage(msgInfo.SequenceNumber); //mark the message for deletion
}

client.CommitDeletes(); //now delete the marked messages for deletion