[英]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.