电子邮件发送失败,通过c#中的循环(使用.net smtp)

时间:2015-03-11 10:57:20

标签: c# .net email smtp

第一封电子邮件已成功发送,其他电子邮件收到错误。

堆栈追踪 -

 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.SmtpConnection.GetConnection(ServicePoint servicePoint)
       at System.Net.Mail.SmtpClient.Send(MailMessage message)

请帮我解决这个问题。

这是我的代码。

SmtpClient SmtpServer = new SmtpClient(System.Configuration.ConfigurationSettings.AppSettings["SMTP_server"]);
                                    //email port
                                    SmtpServer.Port = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["SMTP_Port"]);
                                    //mail server credentials
                                    var userName = System.Configuration.ConfigurationSettings.AppSettings["NetworkCredential_userName"];
                                    var password = System.Configuration.ConfigurationSettings.AppSettings["NetworkCredential_Password"];
                                    if (userName.Length == 0 && password.Length == 0)
                                    {
                                        SmtpServer.Credentials = new System.Net.NetworkCredential();
                                    }
                                    else
                                    {
                                        SmtpServer.Credentials = new System.Net.NetworkCredential(userName, password);
                                    }
                                    //ssl availablility
                                    SmtpServer.EnableSsl = Convert.ToBoolean(System.Configuration.ConfigurationSettings.AppSettings["EnableSsl"]);
                                    mail.Priority = MailPriority.High;
                                    SmtpServer.Send(mail);
                                    //disposing attachment after sending
                                    attachment.Dispose();
                                    SmtpServer.Dispose();

2 个答案:

答案 0 :(得分:1)

这是因为远程SMTP服务器防火墙规则。服务器规则突然改变,它会影响我的代码。实际上我做的是我的线程的睡眠时间增加,它开始工作正常。

答案 1 :(得分:0)

尝试以下但仅限本地机器,不应该是生产环境 -

的情况

在主机文件中将127.0.0.1指向您的机器名称和

代码 -

var client = new SmtpClient("local machine name")
client.Send(mail)