ImapX Latest Sample Code to Get only 1 Unread Email

Topics: Help requests
Jun 8, 2013 at 6:44 PM
Edited Jun 15, 2013 at 2:42 PM
Hi All,

I am looking for the latest sample code preferably C#.Net for using ImapX which receive only 1 Unread item including message body from the mail box and mark it as read. I don't want to get all emails because if there are more number of Items it will stall.

Waiting for your positive response

Regards
Coordinator
Jun 11, 2013 at 9:51 AM
Hi Nabeel,

at the moment it's a little complicated to get only one message. What you can do, is limiting your request to retrieve only the messages which have the Recent flag set:
var messages = myFolder.Search("RECENT");
another options is to use the NEW query, which will retrieve all recent messages, which are not marked as seen:
var messages = myFolder.Search("NEW");
This will allow you to request a minimum of messages. After you received the messages, you can use the AddFlag method on a message to mark it as read, so it won't be included in the next request.

Best regards,

Pavel Azanov
Jun 15, 2013 at 11:33 AM
Edited Jun 15, 2013 at 2:42 PM
Hi Pavel,

Thanks for your input. But it would be grateful if you can help me with this piece of code. What I am trying to do is to retrieve one email Content by MessageUID but It is not working. I don't want to retrieve all the USEEN emails , so I am just trying to pass the MessageUID in second search command to get the content of just 1 email at a time. But Most of the times UID sent comes with 0 count results. Would you please suggest any improvement is required in this code or any other solution that just give me the 1 unread email
                ImapX.FolderCollection folders = client.Folders;
                ImapX.MessageCollection messages = client.Folders["INBOX"].Search("UNSEEN",false); 

                for (int i = 0; i < messages.Count; i++)
                {
                    ImapX.MessageCollection messageDetails = client.Folders["INBOX"].Search("MessageUid" + messages[i].MessageUid, true);
                    .................................. Rest of the code Goes here ........................
                }
Regards
Coordinator
Jun 15, 2013 at 2:30 PM
Hi Nabeel,

your code requires minor change, the argument name of the second search command is not MessageUid. It's just UID:
ImapX.MessageCollection messageDetails = client.Folders["INBOX"].Search("UID " + messages[i].MessageUid, true);
If you want to build custom search queries, i recommend you to take a look at the specification of the search command to be sure about the argument names.

Best regards,

Pavel Azanov


PS: Please try to use one forum thread next time.
Jun 15, 2013 at 2:42 PM
Edited Jun 15, 2013 at 2:43 PM
Hi Pavel,

Thanks for the quick response, However I have already tried with this "UID" as well. But it doesn't work always. I would say 4 out of 10 times. Is there any way to retrieve the single unseen email. If any code snippet is available please do share with me.
Coordinator
Jun 17, 2013 at 7:57 AM
Hi Nabeel,

it's strange that the UID isn't always returned. I'll test this issue, and let you know if there's a fix needed.

Greets,

Pavel Azanov
Jul 1, 2013 at 3:35 PM
Try to do like this :) this is work for me...


int MsgUId = Convert.ToInt32(rowAll["mailMessageId"].ToString());
string uid = "UID "+MsgId;
ImapX.MessageCollection msgNew = client.Folders[gFolderName].Search(uid, true);
Coordinator
Jul 2, 2013 at 9:53 AM
Hi nabeel5,

i have tested the library with three different servers, and received a valid UID for every message. You're also the only one to report this issue, so the problem might be in your code. Which server do you connect to?

Kind regards,

Pavel
Jul 4, 2013 at 1:59 PM
hi All

i also use this code to get mail message. But some times it will display the correct result, But another time Imapx Exception occurs,"Bad Search Query"
Jul 4, 2013 at 4:16 PM
Hi ,

Actually I was using client.Folders["INBOX"].Search("UNSEEN",true); to retrieve all message parts for all unread emails, but when there are large number of emails it takes lot of time which cause issues for the end users. So I used the UID in search string but it doesn't guarantee the MessageUID every time, so I have to change the code mentioned below and this resolve my issue.

What I am doing now is to retrieve all the Emails but not the complete detail for each item. and then upon specific user's email , I am processing the message to retrieve all the detail for that particular email and did my work.
                ImapX.FolderCollection folders = client.Folders;
                ImapX.MessageCollection messages = client.Folders["INBOX"].Search("UNSEEN",false); 
                for (int i = 0; i < messages.Count; i++)
                {
                    messages[i].Process();
                    // To do with your Message stuff

                }
Coordinator
Jul 5, 2013 at 8:10 AM
tharush wrote:
hi All

i also use this code to get mail message. But some times it will display the correct result, But another time Imapx Exception occurs,"Bad Search Query"
The request throws bad query only if the UID is not numerical, or you're trying to request mails from an unselectable folder.



nabeel5 wrote:
Hi ,

Actually I was using client.Folders["INBOX"].Search("UNSEEN",true); to retrieve all message parts for all unread emails, but when there are large number of emails it takes lot of time which cause issues for the end users. So I used the UID in search string but it doesn't guarantee the MessageUID every time, so I have to change the code mentioned below and this resolve my issue.

What I am doing now is to retrieve all the Emails but not the complete detail for each item. and then upon specific user's email , I am processing the message to retrieve all the detail for that particular email and did my work.
Yeah, guess that's the only way at the moment, but it will change from next release.