The website I've written in ASP.NET MVC provides an option for people to subscribe to get the info emailed to them regularly.
So I wondered what the best way of achieving this would be. essentially, I'll be mailing a lot of people once per week, with custom tweaked content per person's email.
I assume I need a separate program from my MVC content app. So that separate program can run from a windows scheduled task?
That separate scheduled app would then loop through forming the emails to be sent on the set day of the week?
As it's not identical for every person, I assume I'll have to loop around for each person, filling in the pertinent information. I want to make sure I don't do it inefficiently and end up compromising my workplace's SMTP server.
I keep seeing "pickup folder" mentioned, is that the way to go?
Extra, it would be nice if I can use some of the views etc from my mvc app to form part of the email, save me redoing things unnec开发者_JAVA技巧essary.
This is the code I'm using so far, I'm not sure how I would steer that into a pickup folder if that's what I need:
internal static bool SendEmail(MailAddress fromAddress, MailAddress toAddress, string subject, string body)
{
try
{
using (var client = new SmtpClient("smtp.server.gov.uk"))
{
client.Send(new MailMessage(fromAddress, toAddress) { Subject = subject, Body = body });
}
return true;
}
catch (SmtpException ex)
{
throw new ApplicationException
("SmtpException has occured: " + ex.Message);
}
catch (Exception)
{
return false;
//throw;
}
}
One interesting approach is to rely on the CacheExpiration to invalidate the cache and let that invalidation execute a piece of code to perform a background task.
Also for sending emails specifically, I would recommend creating new threads and letting each thread do a chunk of the work and then report back on the status where the sent emails can be marked in email. During checkpoints you can update the database with the "sent" markers.
Take a look at this article where the approach is explained in detail: https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
Look at MVCMailer here and I am sure that can solve a lot of your problems.
http://www.nuget.org/packages/MvcMailer
or just
Install-Package MvcMailer
from package manager console.
精彩评论