Click here to Skip to main content
16,021,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I need to write a code which can return the List of mail messages of last 1 hour mails only.
But Code is not working fine.

I need help on this one. Any help will be appreciated

Thanks!
Anmol

What I have tried:

C#
public void mail()
        {
            

            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 (cDate(message.Header("Date")) < DateTime.Now cDate(message.Header("Date")) > DateTime.Now.AddHours(-1))
                {
                    //CODE TO ADD SELECTED MAIL INTO  LIST OF MAIL MESSAGE
                }
                
            }
              if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            
        }
Posted
Updated 27-Jan-19 21:20pm
v2

1 solution

I can recommend this library: GitHub - jstedfast/MimeKit: A .NET MIME creation and parser library with support for S/MIME, PGP, DKIM, TNEF and Unix mbox spools.[^]

It allows you to get new mail like this:
public static void DownloadNewMessages()
        {
            using (var client = new ImapClient())
            {
                client.Connect(imapHost, port);
                client.Authenticate(userName, passWord);
                client.Inbox.Open(FolderAccess.ReadOnly);

                var uids = client.Inbox.Search(SearchQuery.New);

                foreach (var uid in uids)
                {
                    var message = client.Inbox.GetMessage(uid);

                    // write the message to a file
                    message.WriteTo(string.Format("{0}.eml", uid));
                }

                client.Disconnect(true);
            }
        }

For getting mail after a certain time, see the DeliveredAfter or SentAfter method:
SearchQuery Class[^]
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900