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

Email Sending fail via Loop in c# (using .net 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();

尝试下面但仅限于本地机器,生产环境不应该是这种情况-

在主机文件中,将 127.0.0.1 指向您的机器名称,然后

在代码中-

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

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