遍历SmtpClient.Send() [英] Loop over SmtpClient.Send()

查看:187
本文介绍了遍历SmtpClient.Send()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作有发送电子邮件的uniq向用户列表的ASP.NET(C#)的产品。我的code看起来是这样的:

I'm working on a ASP.NET (C#) product that has to send uniq 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();

我得到假SMTP剪纸测试这个时候这个错误:未能发送mail.Unable将数据写入传输连接:

I get this error when testing this with the "fake smtp" Papercut: Failure sending mail.Unable to write data to the transport connection:

我想要做的是保持SMTP连接开放又名。不必重现握手与每一个电子邮件。

What I want to do is to keep the SMTP-connection open aka. don't have to reproduce the "handshake" with every e-mail.

我不是100肯定,但。如果这一工作?我想我有另外一个项目中,它的实现为这一点。

I'm not 100 sure but. Should this work? I think I have another project where it's implemented as this.

推荐答案

的剪纸图书馆将无法以方便你要找的,因为每次调用时间发送行为将删除当前连接,并建立到服务器的另一个连接,反正做握手。下面是从他们的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:

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.

这篇关于遍历SmtpClient.Send()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆