Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notify C# Client, when SMTP Server receive a new Email

Tags:

I want to get all emails in my ASP.NET application that have a certain CC-recipient. To use this for future emails I didn't want to polling all the time to get them. But I can't find a way, how I can use push to get the emails instantly. Are their any frameworks in C# to help me for this?

I want to connect with my application to a mail server and register a method 'X'. Always when a new message arrived to the mail server, my application have to be notified and my application should execute the method 'X'.

I hope that this is possible with code like this:

void Application_Start() 
{
    ...
    ConnectWithTheSmtpServer();
    RegisterMethodForNotification(DoSomethink);
    ...
}
void DoSomethink(Mail newMail)
{
    // Do Somethink with the mail
}

EDIT:

I did it with the MailSystem.Net. It works very fine and is very easy to implement.

Sample Code:

void Application_Start() 
{
    var worker = new BackgroundWorker();
    worker.DoWork += new DoWorkEventHandler(StartIdleProcess);

    if (worker.IsBusy)
        worker.CancelAsync();

    worker.RunWorkerAsync();
}

private void StartIdleProcess(object sender, DoWorkEventArgs e)
{
    if (_imap != null && _imap.IsConnected)
    {
        _imap.StopIdle();
        _imap.Disconnect();
    }

        _imap = new Imap4Client();
        _imap.ConnectSsl(server-name, 993);
        _imap.Login(username, passwort);

        var inbox = _imap.SelectMailbox("INBOX");

        _imap.NewMessageReceived += new NewMessageReceivedEventHandler(NewMessageReceived);

        inbox.Subscribe();

        _imap.StartIdle();
    }

    public static void NewMessageReceived(object source, NewMessageReceivedEventArgs e)
    {
        // Do something with the source...
    }
like image 681
Arthur S. Avatar asked Sep 09 '11 07:09

Arthur S.


People also ask

What does the notify () method do?

notify() wakes up a single thread that is waiting on this object's monitor. If many threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.

How do I notify a specific thread in C++?

If you want to notify a specific thread, use a separate std::condition_variable for it. Do not use that std::condition_variable for other threads.

What is use of notify in thread?

The notify() method is defined in the Object class, which is Java's top-level class. It's used to wake up only one thread that's waiting for an object, and that thread then begins execution. The thread class notify() method is used to wake up a single thread.


1 Answers

You are approaching this from the wrong angle.

SMTP does not support receiving mail (never mind PUSH mail). POP3 is what you can use for retrieving mail, but it does not have support for PUSH either (so you would have to pull for mail).

The IMAP4 IDLE extension is what most refer to as PUSH mail - so you will need to find a library for C# that supports IMAP4 IDLE. I found some information that will get you going in the right direction (no reason to duplicate it here):

  • Using C# .Net Libraries to Check for IMAP Messages
  • Accessing IMAP in C#

Keep in mind when choosing a solution that it needs to support IDLE. I really like the look of MailSystem.Net as it fulfills your requirements.

Remember that your mail server also needs to have IMAP4 and IMAP4 IDLE enabled. Some mail servers don't support it, so you might be clean out of luck (and will have to use POP3 pulling).

like image 117
Jonathan Dickinson Avatar answered Nov 03 '22 11:11

Jonathan Dickinson