-1

How do I when the expiration period of a registration come to end and the system automatically sends an email to the user.

I thought of creating an app in WindowsForms to monitor this, but I wonder if there is another way.

Thanks!!

  • Record the registration date inside your database and put one IF condition inside your business logic to check if a certain time has passed. – Coke Aug 31 '16 at 17:08

2 Answers2

2

It's probably bad form to refer to other answers but I think Shannon's answer presents a reasonable solution. As he mentioned, you can have the Windows Task Scheduler run the console app once a day (or whatever schedule you want; once a day seems reasonable for this though) without it even displaying a console window.

If you don't want to use that solution, though, you might want to consider a SQL Server job. I admittedly haven't looked too closely into this option myself but it apparently is possible to have SQL Server Agent send emails too, which is obviously part of your requirement.

You could also use a Windows Service I suppose (sometimes people will use a timer to have the service do work periodically) but I've never cared for that solution - you have an extra process hanging around even when it's not doing work (and I suspect that the process you describe would only have to run once a day), plus Windows Services can be a through-and-through pain to debug. On the plus side, though, it's pretty easy to format emails in exactly the way you want them and Windows Services don't require any user interaction.

  • 1
    You can definitely have MS SQL server send emails. After setting it up, it's as simple as 'EXEC msdb.dbo.sp_send_dbmail' with a few parameters – TG01 Aug 31 '16 at 18:01
  • Good to know - I haven't tried it personally (hence the disclaimer in my answer) but from what I've read about it it seems like it could be a reasonable solution to this kind of problem. – EJoshuaS - Stand with Ukraine Aug 31 '16 at 18:03
0

Assuming the registration info is stored in a database - create a console app that checks the DB and sends emails to expired users, then schedule the app to run every day at a specific time using Windows Task Scheduler. It's this easy:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Net;
namespace SO_Question_2
{
class Program
{
    static void Main(string[] args)
    {
        sendEmails();
    }

    private static void sendEmails()
    {
        List<string> emails = new List<string>();
        using (SqlConnection conn = new SqlConnection(@"Your Connection String"))
        {
            conn.Open();
            using (SqlCommand com = conn.CreateCommand())
            {
                com.CommandText = "select user_Email from users where exireDate > @today";
                com.Parameters.AddWithValue("@today", DateTime.Now.Date);
                SqlDataReader read = com.ExecuteReader();
                while (read.Read())
                {
                    emails.Add(Convert.ToString(read[0]));
                }
            }
            conn.Close();
            string smtpAddress = "smtp.mail.yahoo.com";
            int portNumber = 587;
            bool enableSSL = true;

            string emailFrom = "yourAddress@yahoo.com";
            string password = "xxxxxx!";

            string subject = "Hello";
            string body = "Buy a new license!";

            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress(emailFrom);
                foreach (string recipient in emails)
                {
                    mail.To.Add(recipient);
                }
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;
                // Can set to false, if you are sending pure text.


                using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                {
                    smtp.Credentials = new NetworkCredential(emailFrom, password);
                    smtp.EnableSsl = enableSSL;
                    smtp.Send(mail);
                }
            }
        }
    }
}

}

Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21
  • Okay, but is there another way to do this? Without Console App? – Fabrício Oliveira Aug 31 '16 at 17:08
  • You could use triggers in SQL to create the list, but you'd still need some code to send the email. You could run as service or console app, but if you used console app, you wouldn't see the window, it executes in the background from Task scheduler. What's your hesitation with console app? – Shannon Holsinger Aug 31 '16 at 17:12
  • There would be zero reason to run this code in a WinForm environment - there's no need for user interaction, unless you want to hand-pick which people get sent emails. – Shannon Holsinger Aug 31 '16 at 17:15
  • You can add the task manually, or look at this article for doing it programmatically: http://stackoverflow.com/questions/7394806/creating-scheduled-tasks – Shannon Holsinger Aug 31 '16 at 17:30
  • @FabrícioOliveira I agree with Shannon here - using the Windows task scheduler seems sensible. If you disagree, though, you might want to consider a SQL Server job: https://msdn.microsoft.com/en-us/library/ms190268.aspx You could use a Windows Service I suppose (sometimes people will use a timer to have the service do work periodically) but I've never cared for that solution - you have an extra process hanging around even when it's not doing work (and I suspect that the process you describe would only have to run once a day), plus Windows Services can be a through-and-through pain to debug. – EJoshuaS - Stand with Ukraine Aug 31 '16 at 17:44
  • I will try this, thanks!! – Fabrício Oliveira Aug 31 '16 at 17:50
  • Glad to see us all collaborating on different good ideas as opposed to stepping on one another screaming "MY WAY IS BETTER!" It's important to remember in coding that there are usually many way to skin the same cat - some better than others depending on a particular situation. The thought that came to my head is no more valid than the others offered here. – Shannon Holsinger Aug 31 '16 at 18:39