简体   繁体   English

使用ASP.NET发送电子邮件

[英]Sending emails with ASP.NET

I am building a web site, and the client wants a newsletter "system" on it. 我正在建立一个网站,客户希望在其上建立新闻通讯“系统”。 How do I send this kinds of mass (>1000) emails? 如何发送这种批量(> 1000)电子邮件?

I read somewhere that using sendasync method of the smtpclient does the trick. 我在某处读到,使用smtpclient的sendasync方法可以解决问题。 But it constantly gives me an "Email faliure" exception. 但是它不断给我一个“电子邮件失败”例外。 And, I don't know what to do with that right there... 而且,我不知道该怎么办...

So, basically my question is, is it ok to send the emails using the SEND method of the smtpclient, but each mail in it's own thread. 因此,基本上我的问题是,可以使用smtpclient的SEND方法发送电子邮件,但是每个邮件都是它自己的线程。

for eg. 例如

   NewsletterEmail newsletterEmail = new NewsletterEmail(emailAdress[i], mailSubject, mailBody);
    Thread t = new Thread(new ThreadStart(newsletterEmail.MakeAndSendEmail));
    t.IsBackground = true;
    t.Start();

i think , you should rethink about your startegey for sending bulk emails 我认为,您应该重新考虑发送大量电子邮件的方法

Creating > 1000 threads is not a good idea , it may even crash your server or it may make your server respond very slow. 创建> 1000个线程不是一个好主意,它甚至可能使服务器崩溃或使服务器响应速度非常慢。

Tell your client about Constant Contact . 向您的客户介绍“ 持续联系” They are going to handle this much better than you ever could. 他们将比您以往更好地处理此问题。 It's also cheaper than your time. 它也比您的时间便宜。

In the event that fails you have a couple options. 万一失败,您有两种选择。

If they already have an email server, leverage that to do the email broadcasting. 如果他们已经有电子邮件服务器,则可以利用该服务器进行电子邮件广播。 In other words, relay the mail through that server. 换句话说,通过该服务器中继邮件。

If you can't do that, go download a free email server. 如果您不能这样做,请下载免费的电子邮件服务器。 I've been using hMailserver . 我一直在使用hMailserver Set it up and relay through it. 设置并中继。

If you can't do that, write your own SMTP processing engine. 如果不能这样做,请编写自己的SMTP处理引擎。 Don't attempt to send the emails directly from ASP.Net. 不要尝试直接从ASP.Net发送电子邮件。 Queue them up in a database and write a windows service to handle the mail broadcasting. 将它们排队在数据库中,并编写Windows服务来处理邮件广播。

Sending emails can sometimes take several seconds per email. 有时每封电子邮件发送电子邮件可能要花费几秒钟。 This could completely hose your website while it's trying to handle sending 1000 emails. 当它试图处理发送1000封电子邮件时,这可能会使您的网站完全瘫痪。

A number of mail servers are configured with grey listing, meaning that they require you to send the same email twice in order to prove you aren't a spammer. 许多邮件服务器配置有灰色列表,这意味着它们要求您发送两次相同的电子邮件,以证明您不是垃圾邮件发送者。

Next, getting the DNS appropriately set up can be a PITA. 接下来,正确设置DNS可以成为PITA。 Which is why I suggest constant contact . 这就是为什么我建议经常联系 I have one client that took nearly 5 years to finally get their DNS configured; 我有一个花了将近5年时间来最终配置其DNS的客户端。 and yes, I gave them explicit instructions once a year on what to do. 是的,我每年都会给他们一次明确的指示,告诉他们该怎么做。 Reverse DNS is critical. 反向DNS至关重要。

Another thing is that some recipient servers have a limit on the number of threads they will accept from you at once. 另一件事是,某些收件人服务器会限制一次接收的线程数量。 Most mail servers are built to take this into consideration. 大多数邮件服务器的构建都考虑到了这一点。 If you cross that boundary, then the recipient servers will consider you a spammer and take appropriate action. 如果您越过边界,则收件人服务器将认为您是垃圾邮件发送者,并采取适当的措施。

Another problem area is in sending to a bad address, over and over again. 另一个问题是反复发送到错误的地址。 AOL and others will consider you a spammer just for this one thing. AOL和其他人会因为这件事而将您视为垃圾邮件发送者。

Point is, you really don't want to write this yourself. 重点是,您真的不想自己写这个。

Your best bet would be to have a separate process send the emails. 最好的选择是让电子邮件发送一个单独的过程。 Either have it run on a schedule to check for emails that need to be sent (maybe store the emails in a table?), or, if you don't like the scheduled process idea, then you can have a console application that is started by your website. 它可以按计划运行,以检查是否需要发送电子邮件(也许将电子邮件存储在表格中?),或者,如果您不喜欢预定的流程想法,则可以启动一个控制台应用程序通过您的网站。

Something else to keep in mind is that if you are sending too many emails in a short period of time, it becomes very easy to get black-listed and then none of the emails from your domain will make it to any servers that have you black-listed. 还有一点要记住的是,如果您在短时间内发送太多电子邮件,将其列入黑名单非常容易,那么来自您域的电子邮件将不会被发送到任何将您黑名单的服务器上列出。

I've taken two approaches: 我采取了两种方法:

1) Lazy, sloppy approach of configuring the asp.net process timeout long enough to complete via Send 1)懒惰,草率的方法来配置asp.net进程超时足够长的时间来通过发送

2) Create a console app that is spawned by the web app. 2)创建一个由Web应用程序生成的控制台应用程序。

I built a similar system a year or two ago. 我在一两年前建立了一个类似的系统。 There's so many things that can go wrong when you send an email programmatically. 以编程方式发送电子邮件时,有很多事情可能出错。 For this reason, do yourself a favor and seperate the messages from the process of sending them. 因此,请帮自己一个忙,将消息与发送过程分开。 This way, you can "teach" your system that "badEmail@goodDomian.com" should be ignored or other similar situations. 这样,您可以“教导”系统应该忽略“ badEmail@goodDomian.com”或其他类似情况。

You can store the message, subject, or whatever level of seperation of data you desire in the database along with a flag for meta-data like "SentOn", "FailedOn", etc. I sent my message one at a time to allow for individual errors to be stored and/or handled. 您可以将消息,主题或所需的任何数据分离级别存储在数据库中,并带有元数据标志,例如“ SentOn”,“ FailedOn”等。我一次发送一条消息以允许要存储和/或处理的单个错误。 I used SmtpMail.Send() , but whatever method you choose should work as long as you built something smart and recoverable. 我使用了SmtpMail.Send() ,但是只要您构建了可恢复的智能内容,您选择的任何方法都应该起作用。

看看MailChimp API: http//www.mailchimp.com/api/gettingstarted/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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