Click here to Skip to main content
15,947,389 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

I am fetching mail from Gmail using IMAP (In Windows Service) which run on background.
But When loop start to fetch mail It read all mail from starting. But I want that It read mail According to current date.

What query should use for this that response time should be less.

I am using this


public void mail()
            string date = Convert.ToString(DateTime.Today);

            Imap client = new Imap();
            // connect to server
            client.Connect("", 993, SslMode.Implicit);
            // authenticate
            client.Login("", "abc123456");
            // select folder
            ImapMessageCollection messages = client.GetMessageList(ImapListFields.Envelope);
            if (con.State != ConnectionState.Open)
            foreach (ImapMessageInfo message in messages)
               // string todaydate = DateTime.Now.ToString("dd/MM/yyyy");
                if (message.Date.LocalTime.ToShortDateString() == DateTime.Now.Date.ToShortDateString())
                    Insert_mail(message.UniqueId, message.Sender.ToString(), message.To.ToString(), message.Subject, message.Date.LocalTime, client.GetMailMessage(message.SequenceNumber).BodyText,message.HasBodyHtml);
              if (con.State == ConnectionState.Open)

public void Insert_mail(string Uid, string from, string to, string subject, DateTime datetime, string message)

                MySqlCommand cmd = new MySqlCommand();
                cmd.Parameters.AddWithValue("@unique_id", Uid);
                cmd.Parameters.AddWithValue("@sender", from);
                cmd.Parameters.AddWithValue("@reciever", to);
                cmd.Parameters.AddWithValue("@subject", subject);
                cmd.Parameters.AddWithValue("@date", datetime);
                cmd.Parameters.AddWithValue("@message", message);
                cmd.Parameters.AddWithValue("@mail_status", "unread");
                cmd.Connection = con;
                string query = "Insert Ignore into gmail_inbox(unique_id,sender,reciever,subject,date,message,mail_status) values (@unique_id,@sender,@reciever,@subject,@date,@message,@mail_status) ";
                cmd.CommandText = query;
            catch (Exception ex)


Updated 10-Apr-13 22:13pm
Prasad Khandekar 11-Apr-13 4:07am    
Hello Neetesh,

You have posted code for INSERT.
Neetesh Agarwal 11-Apr-13 4:12am    
Yeah , bcz after read mail from IMap I need to store in database. AND I want that Its fetch Only Current That mail , I used IF condition in starting of code as u can see but I know its not good. Please suggest me something.

1 solution

Hello Neetesh,

Use following query to read emails in descending order. Also ensure that you have index on date Column.
SELECT * FROM into gmail_inbox ORDER BY date DESC

If you want to retrieve emails for a particular receiver then use
SELECT * FROM into gmail_inbox WHERE receiver = @receiver ORDER BY date DESC
Use SQLCommand with SQLParameter to supply the value for @receiver.

Share this answer
Neetesh Agarwal 11-Apr-13 4:16am    
Hello Prasad Sir, Thanks for Reply but My Problem is something else. I want while message from gmail Its only scan current date email not whole mail of Inbox(bcz there should be more than 10000) mail . So My need if there is any way that Its read only current date mail
Prasad Khandekar 11-Apr-13 5:24am    
I think I have answered it in your earlier post. Use IMAP Filters to search the mails.
Neetesh Agarwal 11-Apr-13 7:16am    
Thanks... I got the solution.
Neetesh Agarwal 11-Apr-13 4:26am    
It Means It can read mail in just Reverse order.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900