Click here to Skip to main content
Rate this: bad
good
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
 
 

 
//code
public void mail()
        {
            string date = Convert.ToString(DateTime.Today);
 
            Imap client = new Imap();
            // connect to server
            client.Connect("imap.gmail.com", 993, SslMode.Implicit);
            // authenticate
            client.Login("abc@gmail.com", "abc123456");
            // select folder
            client.SelectFolder("Inbox");
            ImapMessageCollection messages = client.GetMessageList(ImapListFields.Envelope);
            if (con.State != ConnectionState.Open)
            {
                con.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)
                {
                    con.Close();
                }
            
        }
public void Insert_mail(string Uid, string from, string to, string subject, DateTime datetime, string message)
    {
       try
       {
 
                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;
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
 
            }
 
    }
Posted 10-Apr-13 22:50pm
Edited 10-Apr-13 23:13pm
v3
Comments
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
good
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.
 
Regards,
  Permalink  
Comments
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. http://www.codeproject.com/Questions/574133/Date-time-from-current-time-to-two-days-ago.aspx. 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 325
1 DamithSL 300
2 Sergey Alexandrovich Kryukov 289
3 CPallini 235
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2014
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