Save mail we had read and load only new email in C#
I write a "email client" program to check and read mail. I had download source-code of many author (in codepro开发者_C百科ject)but they program was loading very long and when i restart program must download it again. I have idea to save mail we have downloaded and only download only new mail. How can i save mail? I can use XML or SQL? I using POP3 and IMAP.
Check out Limisoft library.
Here is the sample of code:
using LumiSoft.Net.IMAP;
using LumiSoft.Net.IMAP.Client;
using LumiSoft.Net;
...
using (IMAP_Client client = new IMAP_Client())
{
client.Connect("imap.gmail.com", 993, true);
client.Login("your.username@gmail.com", "your_cool_password");
client.SelectFolder("INBOX");
IMAP_SequenceSet sequence = new IMAP_SequenceSet();
//sequence.Parse("*:1"); // from first to last
IMAP_Client_FetchHandler fetchHandler = new IMAP_Client_FetchHandler();
fetchHandler.NextMessage += new EventHandler(delegate(object s, EventArgs e)
{
Console.WriteLine("next message");
});
fetchHandler.Envelope += new EventHandler<EventArgs<IMAP_Envelope>>(delegate(object s, EventArgs<IMAP_Envelope> e){
IMAP_Envelope envelope = e.Value;
if (envelope.From != null && !String.IsNullOrWhiteSpace(envelope.Subject))
{
Console.WriteLine(envelope.Subject);
}
});
// the best way to find unread emails is to perform server search
int[] unseen_ids = client.Search(false, "UTF-8", "unseen");
Console.WriteLine("unseen count: " + unseen_ids.Count().ToString());
// now we need to initiate our sequence of messages to be fetched
sequence.Parse(string.Join(",", unseen_ids));
// fetch messages now
client.Fetch(false, sequence, new IMAP_Fetch_DataItem[] { new IMAP_Fetch_DataItem_Envelope() }, fetchHandler);
// uncomment this line to mark messages as read
// client.StoreMessageFlags(false, sequence, IMAP_Flags_SetType.Add, IMAP_MessageFlags.Seen);
}
Bit complicated, but works fine. Limisoft library is not perfect, so be sure you test it well.
Messages on the server have a UID (at least for IMAP.) By definition the UID will only increase. Meaning if you have a UID of 321, the next UID has to be 322 or higher. So you could store the "Last UID" in your DB, and then get the highest UID from the server. If it's higher than your last one, load the new ones.
精彩评论