0
using System;
using Limilabs.Mail;
using Limilabs.Client.POP3;

class Program
{
    static void Main(string[] args)
    {

        using (System.IO.StreamWriter file = new     System.IO.StreamWriter(@"C:\Users\*******\Desktop\WriteLines.txt", true)){

            using (Pop3 pop3 = new Pop3())
            {

                Console.WriteLine("Connecting...");

                pop3.ConnectSSL("pop3.live.com");  // or ConnectSSL for SSL 
                pop3.Login("****@live.com", "****");

                // Receive all messages and display the subject 
                MailBuilder builder = new MailBuilder();
                foreach (string uid in pop3.GetAll())
                {
                    IMail email = builder.CreateFromEml(
                    pop3.GetMessageByUID(uid));

                    file.WriteLine("Header: " + email.Subject);
                    file.WriteLine("Message: " + email.Text);

                }

                pop3.Close();

            }

        }   
    }
}    

So I have this problem where I want to have this program running 24/7; it is still incomplete but this is just to show basic C# retrieving emails and such. How can I have a loop in C# that only gets halted or forced to anything only when a new email arrives in the inbox? I would like the program only to actually do something upon getting an email sent to in in realtime. I know I can go and print out my emails one by one all at once but I want it to be running and only when a new message is received do I want i to do anything. For example idk, if I was to send a message like clean desktop, it would stop and be parsed and if a valid command sequence specified by another program I am going to make, then it will carry out that command and then keep on looping endlessly waiting for another new message. It'll basically be a server running 24/7 that only responds to new emails. That is what I am going for. All help is greatly appreciated.

Sacred
  • 147
  • 1
  • 12
  • 4
    "How can I have a loop in C# that only gets halted or forced to anything only when a new email arrives in the inbox." – this is something your email library would have to support directly. (Which I don't actually think is possible in POP3.) Otherwise your only option is polling. – millimoose Feb 05 '14 at 20:54
  • Fairly large amount of code followed by a wall of text. This makes for a hard to read question. Start by breaking up your text into paragraphs? Also, be sure your code example is minimal (while still including all relevant details). – Tim S. Feb 05 '14 at 20:58

2 Answers2

0

Pop3 is a polling protocol. It does not allow to inform you that a new mail has arrived. You would have to e.g. the Exchange protocol that supports this afaik. Thus, for POP3 mailboxes, mail programs usually poll for new emails every e.g. 15min. I'd suspect that your POP3-library does not support this directly. Thus, you would have to use a Timer class and poll for new emails.

Documentation for the Timer class can be found in the MSDN library: http://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx

Georg
  • 5,626
  • 1
  • 23
  • 44
0

You would probably want to use a windows service that polls your email inbox.

A similar question was asked about how to do this here. One of the answers provides a good layout in how to do this.

In addition, if you are trying have your website perform this functionality (because you don't have access to install a windows service) you could use Quartz.net highlighted in this question.

Community
  • 1
  • 1
FrankO
  • 2,522
  • 6
  • 24
  • 34