为什么我不能在C#中的一行发送两个邮件? [英] Why can't i send two mails in a row with c#?
问题描述
我送使用使用SmtpClient C#电子邮件。我不得不每天发送aproximately百不同的电子邮件,并根据收件人我不能使用相同的邮件(添加多个收件人),因为电子邮件的变化。
我不使用一个本地的SMTP服务器,我的理解(根据@rizzle响应的这里)
的一段时间了。不过,我睡我的程序10秒,不过,这仅仅是被发送了第一封电子邮件,从来没有第二个(到目前为止我想有两个电子邮件,而不是一百我的系统)。这是我的代码,任何想法?
的foreach(在clientList人P)
{
AlternateView普莱恩维尔= AlternateView.CreateAlternateViewFromString(Texto可见对clientes阙无蒂嫩HTML,NULL,text / plain的);
// AlternateView htmlView = AlternateView.CreateAlternateViewFromString(这是一个嵌入式图像和LT; IMG SRC = CID:&CompanyLogo的GT;空的text / html);
串htmlString =邮件的HTML字符串身体;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlString,空,text / html的);
System.Net.Mail.MailMessage消息=新System.Net.Mail.MailMessage();
message.AlternateViews.Add(htmlView);
message.To.Add(p.email.Trim());
message.Subject = p.nombre +,邮件主题;
message.From =新System.Net.Mail.MailAddress(FROMADDRESS);
System.Net.Mail.SmtpClient客户端=新System.Net.Mail.SmtpClient(...);
的NetworkCredential myCreds =新的NetworkCredential(USR,传球,);
client.Credentials = myCreds;
client.Send(消息);
System.Threading.Thread.Sleep(10000);
}
执行 client.Dispose()
您发送最后一条消息之后,如果你在DOTNET 4.这将迫使DOTNET SMTP东西完成其工作。
在这里看到:的 System.Net.Mail MAILMESSAGE和不发送消息立即
如果您在早期版本的DOTNET的,尝试做一些事情。
不要 message.Dispose()
当你与消息实例完成。
定义你的本地SmtpClient的方法(内),并退出当你完成发送方法。也就是说,不要试图围绕保持你的客户实例作为你的长寿命类实例的一个领域;它不会刷新,直到它完成您向服务器发送的最后一条消息。
(他们真的在DOTNET 4.0解决这个问题)
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);
}
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)
这篇关于为什么我不能在C#中的一行发送两个邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!