使用IMAP从Gmail读取邮件 [英] Reading Mail From Gmail Using IMAP
本文介绍了使用IMAP从Gmail读取邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友们好,
我使用在后台运行的IMAP(在Windows服务中)从Gmail获取邮件。
但是当循环开始获取邮件它从开始读取所有邮件。但我希望它根据当前日期阅读邮件。
应该使用什么查询,响应时间应该更少。
我正在使用这个
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
{
}
}
推荐答案
Hello Neetesh,
使用以下查询按降序阅读电子邮件。还要确保您有日期索引列。
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
使用SQLCommand和SQLParameter为@receiver提供值。
问候,
Use SQLCommand with SQLParameter to supply the value for @receiver.
Regards,
这篇关于使用IMAP从Gmail读取邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文