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!!
答案 0 :(得分: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 :(得分: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);
}
}
}
}
}
}