Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# MySQL WinForm
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(&quot;@unique_id&quot;, Uid);
            cmd.Parameters.AddWithValue(&quot;@sender&quot;, from);
            cmd.Parameters.AddWithValue(&quot;@reciever&quot;, to);
            cmd.Parameters.AddWithValue(&quot;@subject&quot;, subject);
            cmd.Parameters.AddWithValue(&quot;@date&quot;, datetime);
            cmd.Parameters.AddWithValue(&quot;@message&quot;, message);
            cmd.Parameters.AddWithValue(&quot;@mail_status&quot;, &quot;unread&quot;);
            cmd.Connection = con;
            string query = &quot;Insert Ignore into gmail_inbox(unique_id,sender,reciever,subject,date,message,mail_status) values (@unique_id,@sender,@reciever,@subject,@date,@message,@mail_status) &quot;;
            cmd.CommandText = query;
        catch (Exception ex)
Posted 10-Apr-13 22:50pm
Edited 10-Apr-13 23:13pm
Prasad Khandekar at 11-Apr-13 4:07am
Hello Neetesh,

You have posted code for INSERT.
Neetesh Agarwal at 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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

Neetesh Agarwal at 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 at 11-Apr-13 5:24am
I think I have answered it in your earlier post. Use IMAP Filters to search the mails.
Neetesh Agarwal at 11-Apr-13 7:16am
Thanks... I got the solution.
Neetesh Agarwal at 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)

  Print Answers RSS
0 OriginalGriff 475
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 290
3 ProgramFOX 265
4 CHill60 200
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210

Advertise | Privacy | Mobile
Web03 | 2.8.150331.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100