How to check for new mail?

Jan 15, 2011 at 10:08 AM

I have two questions:

First, is it better to make calls within a using statement so that the mail connection is cleaned up after each call - or to leave the connection open for the lifetime of the application?

Secondly, I use ExecuteList() to get a list of all the e-mails. If I send a new e-mail to that account (from my mail client) - ExecuteList() never reports back that there is a new message. It will continue to say "0" for example. I have to shutdown and restart the application - on the first call to ExecuteList(), it now sees there is a new message. Is there anything I can do or is there a better way to monitor for new messages?

thanks

Coordinator
Jan 16, 2011 at 11:37 PM

Hi.
My assumption is that.
I think that you woud better to minimize the time and duration connecting to server.
Because during connecting to server,other connection is refused.
We can establish one POP3 connection depend on POP3 specification.
So,you can not get real time total mail count without reconnect to server.




Jan 17, 2011 at 2:53 AM

higty,

I am mainly connecting to hotmail - and I saw some unusual behavior, which I think is from the mail server. I would let my application run, which checks for mail every 15 minutes. Here is the working code (for the benefit of others:

    using (Pop3Client client = new Pop3Client())
    {
        client.ServerName = "pop3.live.com";
        client.Port = 995;
        client.Ssl = true;
        client.UserName = "super-duhveloper@hotmail.com";
        client.Password = "passwordgoeshere";
        client.Authenticate();


        List<List.Result> items = client.ExecuteList();

        foreach (List.Result item in items)
        {
            Pop3Message message = client.ExecuteTop(item.MailIndex, 10);
            if (!CheckForExistingMail(item.MailIndex))
            {
                mailItems.Add(new MailItem()
                {
                    From = message.From,
                    Body = message.BodyText,
                    Subject = message.Subject,
                    ItemDate = message.Date.ToLocalTime(),
                    MailId = item.MailIndex
                });
            }
        }
    }

this is where mailItems is an ObservableCollection that is bound to a WPF ListBox. Like I said, this would run every 15 minutes. It would pick up SOME e-mails, but not others. Also, if I send myself an e-mail (from another e-mail account), sometimes it just wouldn't pick it up, but would pick up other messages. Odd.

Tonight though, it has been acting somewhat consistent. Perhaps I was trying to query for new mail too often? Not sure if Hotmail has any caching or limitations about how often you can go check.

Also, I agree - since I am going to have this check every :15 minutes, I just wrapped this in a using block so that the resources are cleaned up and I get a new connection when I need it, later. Thanks much

-Rob

Coordinator
Jan 19, 2011 at 3:50 PM

Hi Rob.
At first,thank you for sharing your information and experience.

And reply is below.
You should better use UIDL with calling ExecuteUidl method.
The MailIndex is not unique in entire lifecycle of mail.
So,CheckForExistingMail method probably does not work as you expected.
For example,you delete your mail at index 10,mails after index 10 is decremented the index.

Another advice is that you would check the result of Authenticate by check the return value of this method.
And be sure to catch exception(if you need) when calling ExecuteList or ExecuteTop.
because sometimes the network connection is not available or remote server does not available.