简体   繁体   English

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

[英]c# smtpException: Failure sending mail - Unable to read data from the transport connection: net_io_connectionclosed

first off, I have searched extensively, havent found a resoltion yet 首先,我进行了广泛搜索,还没有找到解决方法

I have tried using gmail (with normal and 2 way verification password and app access) and yahoo accounts, both which fail with the same exception 我曾尝试使用gmail(具有普通和2种方式的验证密码和应用访问权限)和yahoo帐户,但均因相同的异常而失败

please note: 请注意:

  1. commented out code was of a recent attempt of sending mail with a diiferent method, which also failed 注释掉的代码是最近使用不同方法发送邮件的尝试,但也失败了
  2. the loop in sendmail() is for testing purposes only sendmail()中的循环仅用于测试目的
  3. I have also completly disabled my antivirus, firewall and application blocker for this test 我还完全禁用了此测试的防病毒,防火墙和应用程序阻止程序
  4. System used is Windows 10 使用的系统是Windows 10

this is my error: 这是我的错误:

> 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类:

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);
    }

}

calling method: 调用方法:

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();
        }

adapted for your needs, hope it helps 适应您的需求,希望对您有所帮助

class mail_core
{
    //Mail components
    static MailAddress sFrom;
    static MailAddress sTo;
    static MailMessage newEmail;

    //smtpServer
    SmtpClient SMTPServer;
    static string smtpServerAddress;
    static int smtpServerPort;
    static NetworkCredential cred;
    static bool ssl;

    /*
     *  
     *  Proposed usage:
     *  
     *  mail_core NewEmailToSend = new mail_core();
        temp.NewMail("xyz@gmail.com", "xyz", "abc@rocketmail.com", "abc", "subject string", "body of email, this is my first test email regards", "");
        temp.smtpServerSettings("smtp.gmail.com", 587, "xyz@gmail.com", "your password", true);
     */

    public mail_core() { }


    public void NewMail(string recieverEmail, string recieverName, string senderEmail, string senderName, string subject, string message, string attachementFile)
    {
        /*
        * NewMail creates new mail message, 
        * Necassary parameters are: recieverEmail, senderEmail, subject, message
         * 
         * Optional (can be empty) parameters: recieverName, senderName, attachmentFile
        */
        sTo = new MailAddress(recieverEmail, recieverName);
        sFrom = new MailAddress(senderEmail, senderName);
        newEmail = new MailMessage(sFrom, sTo) 
        {
            Subject = subject,
            Body = message  
        };                
        if (!attachementFile.Equals("")) newEmail.Attachments.Add(new Attachment(attachementFile));
    }


    public void smtpServerSettings(string server, int port, string EmailUsername, string EmailPassword, bool sslEnable)
    {
        /*
         * Enters SMTP (senders) server settings, if wrong, email wont send
        * * Necassary are: ALL
        */
        smtpServerAddress = server;
        smtpServerPort = port;
        cred = new NetworkCredential(EmailUsername, EmailPassword);
        ssl = sslEnable;
    }

    public void addAttachment(string filename)
    {
        /*
        * Add 1 or many attachments
        */
        newEmail.Attachments.Add(new Attachment(filename));
    }


    public bool sendMail()
    {
        /*
         * sends the email
        * return true if sent, returns false if not sent
        */
        try
        {
            SMTPServer = new SmtpClient(smtpServerAddress);
            SMTPServer.Port = smtpServerPort;
            SMTPServer.UseDefaultCredentials = false;
            SMTPServer.Credentials = cred;
            SMTPServer.EnableSsl = ssl;
            SMTPServer.DeliveryMethod = SmtpDeliveryMethod.Network;
            SMTPServer.Send(newEmail);
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 smtp.office365.com 发送邮件失败。 无法从传输连接读取数据:net_io_connectionclosed - smtp.office365.com Failure sending mail. Unable to read data from the transport connection: net_io_connectionclosed SMTPException:无法从传输连接读取数据:net_io_connectionclosed - SMTPException : Unable to read data from the transport connection: net_io_connectionclosed SmtpException:无法从传输连接读取数据:.net_io_connectionclosed - SmtpException: Unable to read data from the transport connection: net_io_connectionclosed 如何解决无法从传输连接读取数据:使用 SMTPClient 发送 email 时出现 net_io_connectionclosed 错误 - How to resolve Unable to read data from the transport connection: net_io_connectionclosed error while sending email using SMTPClient goDaddy SMTP-无法从传输连接中读取数据:net_io_connectionclosed - goDaddy SMTP-Unable to read data from the transport connection: net_io_connectionclosed Gmail:无法从传输连接读取数据:.net_io_connectionclosed - Gmail: Unable to read data from the transport connection: net_io_connectionclosed SendGrid 无法从传输连接读取数据:net_io_connectionclosed - SendGrid Unable to read data from the transport connection: net_io_connectionclosed 错误:无法从传输连接net_io_connectionclosed读取数据 - Error: unable to read data from the transport connection net_io_connectionclosed 无法从传输连接读取数据:net_io_connectionclosed smtp.office365.com - Unable to read data from the transport connection: net_io_connectionclosed smtp.office365.com System.Net.Mail.SmtpException:发送邮件失败。 无法从传输连接中读取数据 - System.Net.Mail.SmtpException: Failure sending mail. Unable to read data from the transport connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM