How can send email in angular based on schedule time


Click Here for Clone Code

Automatically send emails daily at specific time using Windows Service in C# and VB.Net

You can use node-schedule package from npm node-schedule, it will help you to schedule cron jobs whenever you want to send email.

const schedule = require('node-schedule')

const job = schedule.scheduleJob('21 * * * *', function(){
  console.log('Send my email.')
})

This will excute this cron at exactly 21min of any hour.

You can go through the npm package for more scheduling details.


For Example--


private void SchedularCallback(object e)
{
    try
    {
        DataTable dt = new DataTable();
        string query = "SELECT Name, Email FROM Employes WHERE DATEPART(DAY, BirthDate) = @Day AND DATEPART(MONTH, BirthDate) = @Month";
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@Day"DateTime.Today.Day);
                cmd.Parameters.AddWithValue("@Month"DateTime.Today.Month);
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    sda.Fill(dt);
                }
            }
        }
        foreach(DataRow row in dt.Rows)
        {
            string name = row["Name"].ToString();
            string email = row["Email"].ToString();
            WriteToFile("Trying to send email to: " + name + " " + email);
 
            using (MailMessage mm = new MailMessage("sender@gmail.com", email))
            {
                mm.Subject = "Birthday Greetings | For Employes";
                mm.Body = string.Format("<b>Happy Birthday </b>{0}<br /><br />Many happy returns of the day.", name);
 
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
                credentials.UserName = "sender@gmail.com";
                credentials.Password = "<Password>";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = credentials;
                smtp.Port = 587;
                smtp.Send(mm);
                WriteToFile("Email sent successfully to: " + name + " " + email);
            }
        }
        this.ScheduleService();
    }
    catch (Exception ex)
    {
        WriteToFile("Simple Service Error on: {0} " + ex.Message + ex.StackTrace);
 
        //Stop the Windows Service.
        using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("SimpleService"))
        {
            serviceController.Stop();
        }
    }
}

No comments:

Post a Comment

CPU vs GPU Architecture

  CPU vs GPU Architecture CPU (Central Processing Unit) and GPU (Graphics Processing Unit) have distinct architectural differences, optimize...

Best for you