循环 SmtpClient.Send()

发布于 2025-01-01 02:28:35 字数 591 浏览 0 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑依然 2025-01-08 02:28:35

Papercut 库将无法促进您正在寻找的行为,因为每次您调用 Send 时,它都会断开当前连接并建立与服务器的另一个连接,并无论如何都会进行握手。以下是来自他们的 CodePlex 存储库的源代码:

public void Send()
{
    string response;

    Connect(session.Sender, 25);
    response = Response();
    if (response.Substring(0, 3) != "220")
        throw new SmtpException(response);

    Write("HELO {0}\r\n", Util.GetIPAddress());
    response = Response();
    if (response.Substring(0, 3) != "250")
        throw new SmtpException(response);

    Write("MAIL FROM:<{0}>\r\n", session.MailFrom);
    response = Response();
    if (response.Substring(0, 3) != "250")
        throw new SmtpException(response);

    session.Recipients.ForEach(address =>
        {
            Write("RCPT TO:<{0}>\r\n", address);
            response = Response();
            if (response.Substring(0, 3) != "250")
                throw new SmtpException(response);
        });

    Write("DATA\r\n");
    response = Response();
    if (response.Substring(0, 3) != "354")
        throw new SmtpException(response);

    NetworkStream stream = GetStream();
    stream.Write(session.Message, 0, session.Message.Length);

    Write("\r\n.\r\n");
    response = Response();
    if (response.Substring(0, 3) != "250")
        throw new SmtpException(response);

    Write("QUIT\r\n");
    response = Response();
    if (response.IndexOf("221") == -1)
        throw new SmtpException(response);
}

您当然可以在考虑到它是开源之后更改源代码以执行您的操作。

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:

public void Send()
{
    string response;

    Connect(session.Sender, 25);
    response = Response();
    if (response.Substring(0, 3) != "220")
        throw new SmtpException(response);

    Write("HELO {0}\r\n", Util.GetIPAddress());
    response = Response();
    if (response.Substring(0, 3) != "250")
        throw new SmtpException(response);

    Write("MAIL FROM:<{0}>\r\n", session.MailFrom);
    response = Response();
    if (response.Substring(0, 3) != "250")
        throw new SmtpException(response);

    session.Recipients.ForEach(address =>
        {
            Write("RCPT TO:<{0}>\r\n", address);
            response = Response();
            if (response.Substring(0, 3) != "250")
                throw new SmtpException(response);
        });

    Write("DATA\r\n");
    response = Response();
    if (response.Substring(0, 3) != "354")
        throw new SmtpException(response);

    NetworkStream stream = GetStream();
    stream.Write(session.Message, 0, session.Message.Length);

    Write("\r\n.\r\n");
    response = Response();
    if (response.Substring(0, 3) != "250")
        throw new SmtpException(response);

    Write("QUIT\r\n");
    response = Response();
    if (response.IndexOf("221") == -1)
        throw new SmtpException(response);
}

You could of course change the source to do what you are after considering it is open source.

长不大的小祸害 2025-01-08 02:28:35

我猜这可能与 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文