繁体   English   中英

循环 SmtpClient.Send()

[英]Loop over 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();

使用“假 smtp”剪纸测试时出现此错误:发送邮件失败。无法将数据写入传输连接:

我想要做的是保持 SMTP 连接又名打开。 不必在每封电子邮件中重现“握手”。

我不是 100 肯定但是。 这应该工作吗? 我想我还有另一个项目是这样实现的。

我想这可能与 smtp 客户端发送批量邮件的限制有关。 也许您可以在 20 到 30 封邮件后不时处理客户端?

答案来自: 发送邮件失败。 无法将数据写入传输连接

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);
}

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

暂无
暂无

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

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