1

I'm trying to realize a web application with asp.net mvc in which I must receive email 24/7 and do some treatment and notify the specific user .My question is how can I run a thread all time even if the user is not logged.

for the email reception I use S22.Imap:

AutoResetEvent ReconnectEvent = new AutoResetEvent(false);
ImapClient Client;

while (true)
{
    Client = new ImapClient("imap.gmail.com", 993, "login", "password", AuthMethod.Login, true);
    ReconnectEvent.WaitOne();
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    what do you do when an email is received? Could this be better served by a service running alongside the website with some form of shared storage (db etc)? – James Thorpe Jul 22 '16 at 14:22
  • 1
    Generally it's better to offload this kind of constant work into a separate service (a different program). If you want it to run within the context of the web applicaiton there's a number of pitfalls, and you'll find [How to run Background Tasks in ASP.NET by Scott Hanselman](http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx) very useful. – mason Jul 22 '16 at 14:22
  • i already think about that, but how can i build a notification system in that case ? –  Jul 22 '16 at 14:23
  • Well if the user isn't on the site, how could you notify them via the site anyways? So you'll need some sort of persistent storage (like a database or message queue) to store notifications until the user logs in. Once they log in, you can show them. – mason Jul 22 '16 at 14:26
  • yes i know, but when the user is logged ? –  Jul 22 '16 at 14:27
  • i store notification in database no matter what the user is there or not but i need to send notification when he/she is logged –  Jul 22 '16 at 14:29
  • When they access the site you can read from the storage to see if any notifications are awaiting. If so, display them to user. – mason Jul 22 '16 at 14:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118033/discussion-between-boughti-and-mason). –  Jul 22 '16 at 14:30

1 Answers1

0

The best and the most efficient way of doing this will be by creating a separate service rather than keeping it within the asp.net app.

if you still wants to use it inside the app then explore the package called WEBBACKGROUNDER to run the task in the background.

hope this helps.

Waqar
  • 210
  • 2
  • 12