c#smtpException:发送邮件失败 - 无法从传输连接读取数据:net_io_connectionclosed

时间:2015-08-09 12:45:56

标签: c# email smtp gmail smtpclient

首先,我已广泛搜索,尚未找到解决方案

我尝试过使用gmail(使用普通和双向验证密码和应用访问权限)和yahoo帐户,两者都失败并出现相同的异常

请注意:

  1. 注释掉了代码最近尝试使用不同的方法发送邮件,但也失败了
  2. sendmail()中的循环仅用于测试目的
  3. 我还完全禁用了此测试的防病毒,防火墙和应用程序拦截器
  4. 使用的系统是Windows 10
  5. 这是我的错误:

    > 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)
       at ConsoleApplication1.mail_core.sendMail() in c:\Users\CybeX\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\mail_core.cs:line 69
    

    mail_core类:

    class mail_core
    {
        //Mail components
        static MailAddress from;
        static string sfrom;
        static string sto;
        static MailAddress to;
        static MailMessage newEmail;
    
        //smtpServer
        SmtpClient SMTPServer;
        static string smtpServerAddress;
        static int smtpServerPort;
        static NetworkCredential cred;
        static bool ssl;
    
        public mail_core() { }
    
        public void NewMail(string recieverEmail, string recieverName, string senderEmail, string senderName, string subject, string message, string attachementFile)
        {
            sto = recieverEmail;
            sfrom = senderEmail;
            //to = new MailAddress(recieverEmail, recieverName);
            //from = new MailAddress(senderEmail, senderName);
            //newEmail = new MailMessage(from, to);
            //newEmail.Subject = subject;
            //newEmail.Body = message;
            newEmail = new MailMessage(sfrom, sto, subject, message);
            if (!attachementFile.Equals(""))
            {
                newEmail.Attachments.Add(new Attachment(attachementFile));
            }
        }
    
        public void smtpServerSettings(string server, int port, string EmailUsername, string EmailPassword, bool sslEnable)
        {
            smtpServerAddress = server;
            smtpServerPort = port;
            cred = new NetworkCredential(EmailUsername, EmailPassword);
            ssl = sslEnable;
        }
    
        public void sendMail()
        {
            bool sent = false;
            int count = 0;
            while (!sent)
            {
                count++;
                try
                {
                    SMTPServer = new SmtpClient(smtpServerAddress);
                    SMTPServer.Port = smtpServerPort;
                    SMTPServer.UseDefaultCredentials = false;
                    SMTPServer.Credentials = cred;
                    SMTPServer.EnableSsl = ssl;
                    SMTPServer.DeliveryMethod = SmtpDeliveryMethod.Network;
                    SMTPServer.Send(newEmail);
                    sent = true;
                }
                catch (Exception x)
                {
                    Console.WriteLine(x);
                    sent = false;
                }
            }
            Console.WriteLine("tried times: {0}",count);
        }
    
    }
    

    调用方法:

    static void Main(string[] args)
            {
                mail_core temp = new mail_core();
                Console.WriteLine("ready?");
                Console.ReadLine();
                temp.NewMail("xyz@gmail.com", "xyz", "abc@rocketmail.com", "abc", "a test email", "this is my first test email", "");
                temp.smtpServerSettings("smtp.gmail.com", 465, "xyz@gmail.com", "2 step google verification password", true);
                Console.WriteLine("sending...");
                Console.WriteLine("=============================================================");
                temp.sendMail();
                Console.WriteLine("=============================================================");
                //if (temp.sendMail())
                //{
                //    Console.WriteLine("Mail sent :DDDD");
                //}
                //else Console.WriteLine("Mail not sent :(((");
                Console.ReadLine();
            }
    

1 个答案:

答案 0 :(得分:0)

根据您的需求进行调整,希望有所帮助

<?php
    $dbhost = $_POST['dbhost'];
    $dbuser = $_POST['dbuser'];
    $dbpass = $_POST['dbpass'];
    $dbname = $_POST['dbname'];
    $dbcoll = str_ToLower($_POST['dbcoll']);
    $dbcoll_ary = explode("_", $dbcoll);
    $charset = trim($dbcoll_ary['0']);
    $engine = 'innodb';

    if($charset == 'utf8mb4'){
        $engine = 'innodb ROW_FORMAT=DYNAMIC';
    }

    //if you set your $tbl_name array here, it will work.
    include("table_names.php");

   $sql = "CREATE TABLE `$dbname`.`{$tbl_name[$key]['db']}`(" . PHP_EOL;
   foreach(${'tbl_' . $key} as $data){
        $sql .= $data . PHP_EOL;
   } // Closing brace for foreach(${'tbl_' . $key} as $data)

?>