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();
client.Connect("imap.gmail.com", 993, SslMode.Implicit);
client.Login("abc@gmail.com", "abc123456");
client.SelectFolder("Inbox");
ImapMessageCollection messages = client.GetMessageList(ImapListFields.Envelope);
if (con.State != ConnectionState.Open)
{
con.Open();
}
foreach (ImapMessageInfo message in messages)
{
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
{
}
}