Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sent Email at regular Interval to user for Password updation

I have a table created by Identity as AspNetUser.

I want to send an email to corresponding user if he did not update his password for six months. I have to send mail after 60 days and after that every 3 days. I am using .net framework 4.5.2, asp.net, mvc 5, identity2
My model

    public class IdentityUser : IUser
    {
        public IdentityUser();
        public IdentityUser(string userName);
        .
        .
        public DateTime LastUpdateDate{get;set;}
    }

My controller

public ActionResult PasswordSendMail()
{
    my code for checking six month...
}

But the mail has to be sent once at the 60th day from the LastUpdateDate and after every three days.
If i placed the code in controller, the action should be called. I have to send without calling any controller specifically.
I want to sent mail to users, whether user is logged in or not. when the time reached the mail should be sent by background process.
how to integrate the windows service with the current project. Brief answer will be helpful
Thanks in advance

like image 329
anand Avatar asked Feb 22 '16 12:02

anand


2 Answers

You have two primary options here. The first and easiest is to simply not worry about notifying the user exactly at a certain interval, and just notify them upon login if it's time to change the password. You can simply add your code to check if the password is older than the defined timeframe to your login code, and then redirect them to a password change form if it's time. Or, if you really want to be fancy about it, you can create an action filter that does this check and redirects to the password change form, so the user will not be able to do anything else until they've updated their password.

If you really want to send an email on a schedule, then you need something that can run the code on a schedule. You have two options in this regard. First, you can create a console app that contains this code, and then simply use something like Task Scheduler in Windows to run it on a set schedule. Second, you can use something like Revalee to call your action on a schedule.

like image 186
Chris Pratt Avatar answered Nov 09 '22 12:11

Chris Pratt


If you want to schedule actions on your server, I would recommend to create a windows service with a timer that call a url at a given moment. I tried the task scheduler and it was not reliable enough for me.

This is the most important part of your windows service

protected override void OnStart(string[] args)
        {
            LaunchTimer(MilliSecondsToNextLaunch());
        }

protected void LaunchTimer(double interval)
        {
            Timer = new Timer { Interval = interval };
            Timer.Elapsed += OnTimer;
            Timer.Start();
        }

protected override void OnTimer(object sender, ElapsedEventArgs args)
        {
            using (var client = new WebClient())
            {
                client.DownloadString(siteUrl);
            }
            SetIntervalForTimer(MilliSecondsToNextLaunch() + 1000);
        }

protected void SetIntervalForTimer(double nextInterval)
        {
            Timer.Stop();
            Timer.Interval = nextInterval;
            Timer.Start();
        }
like image 29
Daniel Avatar answered Nov 09 '22 12:11

Daniel