【发布时间】:2015-04-06 09:08:01
【问题描述】:
不久前,我已将我的 ASP.NET C# 项目配置为通过 Office 365 发送电子邮件,但上周它开始抛出很多异常。
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.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
如何防止这种情况发生?
MailMessage message = new MailMessage(System.Configuration.ConfigurationManager.AppSettings["smtpfrom"], email, strOnderwerp, strBody);
message.Priority = MailPriority.Normal;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient(System.Configuration.ConfigurationManager.AppSettings["smtpserver"], Convert.ToInt32((System.Configuration.ConfigurationManager.AppSettings["smtpport"])));
client.EnableSsl = Boolean.Parse(System.Configuration.ConfigurationManager.AppSettings["smtpssl"]); ;
client.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["smtpuser"], System.Configuration.ConfigurationManager.AppSettings["smtppass"]);
client.Send(message);
client.Dispose();
Dispose 上似乎抛出了异常。
【问题讨论】:
-
与您的问题无关,但我会从您的电子邮件发送代码中收到
ConfigurationManager的电话。我会编写一个用于发送电子邮件的通用方法(例如名称:SendEmail),并将这些值作为变量传递。将调用ConfigurationManager放在调用SendEmail的任何方法中。嗯......实际上我会将我的ConfigurationManager调用包装在一个特定于返回 Config 值的规范化类中,所以我可以将它编码为变量MyAppConfigClass.SmtpServer