C# smtpclient "发送邮件失败"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2209617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
smtpclient " failure sending mail"
提问by Nnp
here is my code
这是我的代码
for(int i = 0; i < number ; i++)
{
MailAddress to = new MailAddress(iMail.to);
MailAddress from = new MailAddress(iMail.from, iMail.displayName);
string body = iMail.body;
string subject = iMail.sub;
oMail = new MailMessage(from, to);
oMail.Subject = subject;
oMail.Body = body;
oMail.IsBodyHtml = true;
oMail.Priority = MailPriority.Normal;
oMail.Sender = from;
s = new SmtpClient(smtpServer);
if (s != null)
{
?s.Send(oMail);
}
oMail.Dispose();
s = null;
}
this loops sends over 60,000 email. but my problem i am getting " failure sending mail" in some of the email some times 5000 and some time less then that rest of them gets delivered. and i have check all those error out email has valid email address. dont know what is the problem. i really need help in this.
这个循环发送了超过 60,000 封电子邮件。但我的问题是我在某些电子邮件中收到“发送邮件失败”,有时是 5000 封邮件,有时比其余邮件少一些。我已经检查了所有这些错误的电子邮件是否具有有效的电子邮件地址。不知道是什么问题。我真的需要这方面的帮助。
Edit: This is my exception Trace
编辑:这是我的异常跟踪
Error - Failure sending mail.; Inner Ex - System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed. at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
错误 - 发送邮件失败。内部 Ex - System.IO.IOException:无法从传输连接读取数据:net_io_connectionclosed。在 System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory .ReadLine(SmtpReplyReader caller) at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net。 Mail.SmtpTransport.SendMail(MailAddress 发件人、MailAddressCollection 收件人、String deliveryNotify、SmtpFailedRecipientException& 异常)
采纳答案by Nnp
apparently this problem got solved just by increasing queue size on my 3rd party smtp server. but the answer by Nip sounds like it is fairly usefull too
显然这个问题只是通过增加我的 3rd 方 smtp 服务器上的队列大小来解决的。但 Nip 的回答听起来也相当有用
回答by Pharabus
what error do you get is it a SmtpFailedrecipientException? if so you can check the innerexceptions list and view the StatusCode to get more information. the link below has some good information
你得到什么错误是 SmtpFailedrecipientException?如果是这样,您可以检查内部异常列表并查看状态代码以获取更多信息。下面的链接有一些很好的信息
Edit for the new information
编辑新信息
Thisis a problem with finding your SMTP server from what I can see, though you say that it only happens on some emails. Are you using more than one smtp server and if so maybe you can tract the issue down to one in particular, if not it may be that the speed/amount of emails you are sending is causing your smtp server some issue.
这是从我所看到的找到您的 SMTP 服务器的问题,尽管您说它只发生在某些电子邮件上。您是否使用了多个 smtp 服务器,如果是这样,也许您可以将问题具体归结为一个,如果不是,则可能是您发送的电子邮件的速度/数量导致了您的 smtp 服务器出现了一些问题。
回答by Mitchel Sellers
Well, the "failure sending e-mail" should hopefully have a bit more detail. But there are a few things that could cause this.
好吧,“发送电子邮件失败”应该有更多细节。但是有一些事情可能会导致这种情况。
- Restrictions on the "From" address. If you are using different from addresses, some could be blocked by your SMTP service from being able to send.
- Flood prevention on your SMTP service could be stopping the e-mails from going out.
- 对“发件人”地址的限制。如果您使用不同的地址,则某些地址可能会被您的 SMTP 服务阻止而无法发送。
- 您的 SMTP 服务上的防洪措施可能会阻止电子邮件发送出去。
Regardless if it is one of these or another error, you will want to look at the exception and inner exception to get a bit more detail.
无论是这些错误之一还是其他错误,您都需要查看异常和内部异常以获取更多详细信息。
回答by Romhein
I experienced the same issue when sending high volume email. Setting the deliveryMethod
property to PickupDirectoryFromIis
fixed it for me.
Also don't create a new SmtpClient everytime.
我在发送大量电子邮件时遇到了同样的问题。设置deliveryMethod
属性PickupDirectoryFromIis
为我修复它。也不要每次都创建一个新的 SmtpClient。
回答by Marc Roussel
For us, everything was fine, emails are very small and not a lot of them are sent and sudently it gave this error. It appeared that a technicien installed ASTARO which was preventing email to be sent. and we were getting this error so yes the error is a bit cryptic but I hope this could help others.
对我们来说,一切都很好,电子邮件很小,发送的邮件并不多,突然出现了这个错误。看来是技术人员安装了 ASTARO,它阻止了电子邮件的发送。我们收到了这个错误,所以是的,这个错误有点神秘,但我希望这可以帮助其他人。
回答by OM Krishna
Seeing your loop for sending emails and the error which you provided there is only solution.
Declare the mail object out of the loop and assign fromaddress
out of the loop which you are using for sending mails. The fromaddress
field is getting assigned again and again in the loop that is your problem.
查看您发送电子邮件的循环以及您提供的错误,只有解决方案。
在循环外声明邮件对象并fromaddress
在用于发送邮件的循环外分配。该fromaddress
字段在循环中一次又一次地被分配,这就是您的问题。
回答by Daniel Hedenstr?m
This error can appear when the web server can't access the mail server. Make sure the web server can reach the mail server, for instance pinging it.
当 Web 服务器无法访问邮件服务器时,可能会出现此错误。确保 Web 服务器可以访问邮件服务器,例如 ping 它。
回答by Mike Gledhill
Five years later (I hope this developer isn't still waiting for a fix to this..)
五年后(我希望这个开发人员不会还在等待解决这个问题......)
I had the same issue, caused by the same error: I was declaring the SmtpClient
inside the loop.
我有同样的问题,由同样的错误引起:我SmtpClient
在循环内部声明。
The fix is simple - declare it once, outside the loop...
修复很简单 - 在循环之外声明一次......
MailAddress mail = null;
SmtpClient client = new SmtpClient();
client.Port = 25;
client.EnableSsl = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.Host = smtpAddress; // Enter your company's email server here!
for(int i = 0; i < number ; i++)
{
mail = new MailMessage(iMail.from, iMail.to);
mail.Subject = iMail.sub;
mail.Body = iMail.body;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.Normal;
mail.Sender = from;
client.Send(mail);
}
mail.Dispose();
client.Dispose();