循环 SmtpClient.Send()
我正在开发一个 ASP.NET (C#) 产品,该产品必须向订阅者列表发送唯一的电子邮件。我的代码看起来像这样:
// Grab subscribers from db, about 10-20.
var malingList = Bll.GetAllSubscribers();
var client = new SmtpClient();
// Set up settings on the SmtpClient with cridentails and so on
foreach(var subscriber in mailingList)
{
var message = new MailMessage();
// Set up message, set reciver, yada yada
client.Send(message);
}
client.Dispose();
当使用“fake smtp” Papercut 测试此错误时,我收到此错误:发送邮件失败。无法将数据写入传输连接:
我想要做的是保持 SMTP 连接打开。不必在每封电子邮件中都重复“握手”。
我不是百分百确定,但是。这应该有效吗?我想我还有另一个项目是这样实现的。
I'm working on a ASP.NET (C#) product that has to send unique emails to a list of subscribers. My code looks something like this:
// Grab subscribers from db, about 10-20.
var malingList = Bll.GetAllSubscribers();
var client = new SmtpClient();
// Set up settings on the SmtpClient with cridentails and so on
foreach(var subscriber in mailingList)
{
var message = new MailMessage();
// Set up message, set reciver, yada yada
client.Send(message);
}
client.Dispose();
I get this error when testing this with the "fake smtp" Papercut: Failure sending mail.Unable to write data to the transport connection:
What I want to do is to keep the SMTP-connection open aka. don't have to reproduce the "handshake" with every e-mail.
I'm not 100 sure but. Should this work? I think I have another project where it's implemented as this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Papercut 库将无法促进您正在寻找的行为,因为每次您调用
Send
时,它都会断开当前连接并建立与服务器的另一个连接,并无论如何都会进行握手。以下是来自他们的 CodePlex 存储库的源代码:您当然可以在考虑到它是开源之后更改源代码以执行您的操作。
The Papercut library will not be able to facilitate the behavior you're looking for because each time you call
Send
it will drop the current connection and establish another connection to the server and do the handshake anyway. Here's the source from their CodePlex repository:You could of course change the source to do what you are after considering it is open source.
我猜这可能与 smtp 客户端发送批量邮件的限制有关。
也许您可以在 20 到 30 封邮件之后偶尔处理该客户端?
答案来自:
发送邮件失败。无法将数据写入传输连接
I guess it could have some relation with the limitation of the smtp client to send bulk mails.
Maybe you could dispose the client so now and then, after 20-30 mails?
Answer gotten from:
Failure sending mail. Unable to write data to the transport connection