为什么用c#不能连续发送两封邮件?
我正在使用 C# 使用 SmtpClient 发送电子邮件。我每天必须发送大约一百封不同的电子邮件,并且我无法使用相同的邮件(添加多个收件人),因为电子邮件会根据收件人的不同而变化。
我没有使用本地 SMTP 服务器,并且我了解(根据 @rizzle 响应 此处< /a>) 一封邮件与另一封邮件之间必须间隔一段时间。然而,我让我的程序休眠了 10 秒,但仍然只是发送了第一封电子邮件,而不是第二封电子邮件(到目前为止,我正在尝试使用两封电子邮件而不是一百封电子邮件来尝试我的系统)。这是我的代码,有什么想法吗?
foreach (Person p in clientList)
{
AlternateView plainView = AlternateView.CreateAlternateViewFromString("Texto visible para clientes que no tienen HTML", null, "text/plain");
//AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:companylogo>", null, "text/html");
string htmlString = "html string body of the email";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlString, null, "text/html");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.AlternateViews.Add(htmlView);
message.To.Add(p.email.Trim());
message.Subject = p.nombre+", email subject";
message.From = new System.Net.Mail.MailAddress(fromAddress);
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("...");
NetworkCredential myCreds = new NetworkCredential(usr, pass, "");
client.Credentials = myCreds;
client.Send(message);
System.Threading.Thread.Sleep(10000);
}
I am sending emails using c# using SmtpClient. I have to send aproximately one hundred different emails per day, and I can't use the same mail (adding several recipients) since the email changes according to the recipient.
I am not using a local SMTP server, and I understand (according to @rizzle response here)
that some time has to be left between one mail and another one. However, I am sleeping my program for 10 seconds and still, it is only the first email that gets sent, never the second one (so far I am trying my system with two emails instead of one hundred). This is my code, any ideas?
foreach (Person p in clientList)
{
AlternateView plainView = AlternateView.CreateAlternateViewFromString("Texto visible para clientes que no tienen HTML", null, "text/plain");
//AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:companylogo>", null, "text/html");
string htmlString = "html string body of the email";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlString, null, "text/html");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.AlternateViews.Add(htmlView);
message.To.Add(p.email.Trim());
message.Subject = p.nombre+", email subject";
message.From = new System.Net.Mail.MailAddress(fromAddress);
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("...");
NetworkCredential myCreds = new NetworkCredential(usr, pass, "");
client.Credentials = myCreds;
client.Send(message);
System.Threading.Thread.Sleep(10000);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 dotnet 4,请在发送最后一条消息后立即执行
client.Dispose()
。这将强制 dotnet SMTP 内容完成其工作。请参阅此处: System.Net.Mail 和 MailMessage不立即发送消息
如果您使用的是早期版本的 DotNet,请尝试执行以下操作。
完成消息实例后,执行
message.Dispose()
。在本地定义 SmtpClient(在方法内)并在发送完成后退出该方法。也就是说,不要尝试将您的客户端实例保留为长期存在的类实例之一中的字段;在最终确定之前,它不会刷新您发送到服务器的最后一条消息。
(他们确实在 dotnet 4.0 中修复了这个问题)
Do
client.Dispose()
right after you send your last message, if you're on dotnet 4. This will force the dotnet SMTP stuff to finish its work.See here: System.Net.Mail and MailMessage not Sending Messages Immediately
If you're on earlier versions of DotNet, try doing a couple of things.
Do
message.Dispose()
when you're done with the message instance.Define your SmtpClient locally (within a method) and exit the method when you're done sending. That is, don't try to keep your client instance around as a field in one of your long-lived class instances; it won't flush the last message you sent to the server until it's finalized.
(They really did fix this in dotnet 4.0)
在 3.5 中尝试使用:
然后在
SendCompletedCallback
中触发下一个...SmtpClient 类
In 3.5 try using:
Then in the
SendCompletedCallback
trigger the next...SmtpClient Class