SmtpClient"无法从传输连接中读取数据"仅在发送x份电子邮件后才会发生

时间:2018-01-24 09:33:35

标签: c# .net smtp smtpclient smtp-auth

编辑:我尝试过的事情

  1. 在smpt虚拟服务器上运行smtp的服务器上,我设置了IP 地址到特定的IP和#34;所有未分配的",在Access的选项卡中 SMTP虚拟服务,在中继选项卡下,我已将所有IP添加到 授权清单以及127.0.0.1

  2. client.ServicePoint.MaxIdleTime = 100;

  3. 将使用(创建客户端)移动到for循环内部

  4. 仍然是同样的问题,在它出现之前,300似乎是一个神奇的数字

    我有一个非常奇怪的问题,我过去一周一直试图解决。我的任务很简单,"发送批量邮件",所以我创建了一个非常简单的exe,它将一堆电子邮件发送到一个Gmail帐户。
    在发送280之后,发送失败并出现以下错误:我已尝试端口587和25,并且两个端口都出现问题

    System.Net.Mail.SmtpException: Failure sending mail. ---> 
    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.SmtpTransport.GetConnection(ServicePoint servicePoint)
    at System.Net.Mail.SmtpClient.GetConnection()
    at System.Net.Mail.SmtpClient.Send(MailMessage message)
    --- End of inner exception stack trace ---
    at System.Net.Mail.SmtpClient.Send(MailMessage message)
    

    现在我要注意,如果我在失败后再次运行exe,我尝试发送一封电子邮件,我会得到同样的错误,所以问题似乎不是编码问题。这是在运行Windows 2016和SmarterMail 16 smtp服务器的VPS上发送的。

    如果我等了大约一个小时并尝试再次发送,我可以成功发送另一个280.似乎在某处发生某种限制(我已经关闭了SmarterMail中的所有限制)。

    奇怪的是,在它失败之后,如果我使用像https://www.smtper.net/这样的服务并使用我在我的exe中使用的相同的确切设置,那么电子邮件会经历任何错误。我不确定这是否是一个smtp错误,Windows 2016上的一些设置不允许超过x小时等。

    下面是我的实际exe代码,你可以看到它非常简单的例子

    static void Main(string[] args)
        {
            Console.WriteLine("How many emails do you want to send?");
            var emailCount = Convert.ToInt32(Console.ReadLine());
    
            Console.WriteLine("How many seconds do you want to delay between each send");
            var delay = Convert.ToInt32(Console.ReadLine());
    
    
            Console.WriteLine("What email address do you want to send to");
            var toEmailAddress = Console.ReadLine();
    
            Console.WriteLine($"Sending out {emailCount} with {delay} second delay to  {toEmailAddress}");
    
            using (var client = new SmtpClient(ConfigurationManager.AppSettings["SmtpServer"]))
            {
                for (int i = 1; i <= emailCount; i++)
                {
                    try
                    {
                        var body = "this is a test message";
                        var userName = ConfigurationManager.AppSettings["Username"];
                        var password = ConfigurationManager.AppSettings["Password"];
                        client.UseDefaultCredentials = false;
                        client.Credentials = new NetworkCredential(userName, password);
                        client.Port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
                        //client.EnableSsl = true;
    
                        MailMessage mailMessage = new MailMessage();
                        mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["FromEmail"]);
    
                        mailMessage.To.Add(toEmailAddress);
                        mailMessage.Body = body;
                        mailMessage.IsBodyHtml = true;
    
                        mailMessage.Subject = ConfigurationManager.AppSettings["Subject"];
    
                        client.Send(mailMessage);
                        if (delay > 0)
                        {
                            Console.WriteLine("Sleeping...");
    
                            Thread.Sleep(delay * 1000);
                        }
    
                        Console.WriteLine($"Email number {i} was sent successfully.");
    
                    }
                    catch (Exception ex)
                    {
    
                        Console.WriteLine($"Failed to send {i} of {emailCount}");
                        Console.WriteLine(ex.ToString());
    
    
                    }
    
                }
            }
            Console.WriteLine("Done...press any key to exit");
            Console.ReadKey();
    
        }
    

0 个答案:

没有答案