简体   繁体   中英

Sending Email Over SMTP Limit

I maintain the website for a small organization with roughly 100 to 120 members.

Every month, we send out an email notifying our members of the topics we will cover in the upcoming meetings.

I am trying to use our website to give us a way to send these emails out, but our ASP Hosting Site has a defined limit of 50 emails per hour .

Theoretically, I suppose I could put the thread to sleep for 1-minute after each SmtpClient.Send(MailMessage) call by using a Thread.Sleep(60000) , like this:

public const int ONE_MINUTE = 60000;

public String SiteEmailAddress { get; set; }
public String SiteMailServer { get; set; }
public String SiteMailUserId { get; set; }
public String SiteMailPwd { get; set; }

public void BulkEmail(String emailMessage) {
    using (var msg = new MailMessage()) {
        msg.From = new MailAddress(SiteMailAddress);
        msg.Body = emailMessage;
        msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
        using (var server = new SmtpClient(SiteMailServer, 25)) {
            server.Credentials = new NetworkCredential(SiteMailUserId, SiteMailPwd);
            foreach (var person in Personnel.GetActiveMembers()) {
                var msgTo = person.PersonalEmail;
                if (!String.IsNullOrEmpty(msgTo)) {
                    msg.To.Clear();
                    msg.To.Add(new MailAddress(msgTo, person.FullName));
                    server.Send(msg);
                    System.Threading.Thread.Sleep(ONE_MINUTE);
                }
            }
        }
    }
}

That would either make our website appear dead for over an hour while this ran or it would likely fail.

What is a simple solution for doing this?

Don't use your web server to send emails.

Let a mail sending API handle it for you. This isn't the place to recommend a specific product, so I won't.


If you are going to trickle emails out through your web server, don't do it on the web thread. Store your email-sending status in the database and send one at a time, do it on a separate thread, or use a separate app.

Based on your case I would suggest some solutions:
1- Schedule a task to run every 1.2 min for example, and this task will call the email sender method, and not need to use the Sleep.
Example of how to schedule tasks in asp.net website: http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx
Best way to run scheduled tasks
http://hangfire.io/

2- Add a new task in the windows task scheduler manually or using code (if your hosting allow this), and make this task to call a page in your website which sends the email.

3- Create a separate application in a separate thread which handle the email sending process without affecting your main website performance (this could be a windows service, console app, or normal windows app).

If you're determined to send the emails from your web server, you can use Background Tasks in ASP.NET. Scott Hanselman did a nice write up about it. . You could have it process 50 at a time then wait the remainder of the hour before continuing or wait 72 seconds between each email so that you're sending at most 50 an hour.

我们使用sendgrid为我们的网站发送电子邮件,每月免费限制为12,000 https://sendgrid.com/free也许是一个更简单的选择?

First of all creating background thread and put it to sleep does not kill application in any way. It will just take thread from thread pool and lock it till operation complete and than dispose it.

Though this can be issue on high traffic applications and limited thread pool, where multiple requests must be handled on same time. Another thing to consider is with each application shutdown, this task get canceled if running. But if that is not issue for you and you want a simple solution, that is.

I would recommend rather use of external API to let it handle for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM