且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

错误 - 从ASP.NET发送电子邮件

更新时间:2023-01-17 12:39:42

Yahoo SMTP端口是465,而不是25.



您还需要在SmtpClient对象上将EnableSsl设置为true。

Hey Guys,

i am trying to send email using asp.net and i got this SMTPException:

Failure sending mail.
{"Unable to read data from the transport connection: net_io_connectionclosed."}

and Her is My Code:

try
        {
            //YAHOO SMTP Host (smtp.mail.yahoo.com)
            //GMAIL SMTP Host (smtp.gmail.com)
            //WINDOWS LIVE SMTP HOST (smtp.live.com)
            SmtpClient mySmtpClient = new SmtpClient("smtp.mail.yahoo.com", 25);

            // set smtp-client with basicAuthentication
            mySmtpClient.UseDefaultCredentials = false;
            System.Net.NetworkCredential basicAuthenticationInfo = new
               System.Net.NetworkCredential("myEmail", "myPassword");
            mySmtpClient.Credentials = basicAuthenticationInfo;

            // add from,to mailaddresses
            MailAddress from = new MailAddress("From_Email", "Title");
            MailAddress to = new MailAddress("To_Email", "Test Email");
            MailMessage myMail = new System.Net.Mail.MailMessage(from, to);

            // set subject and encoding
            myMail.Subject = Email_Subject;
            myMail.SubjectEncoding = System.Text.Encoding.UTF8;

            // set body-message and encoding
            myMail.Body = Email_Body;
            myMail.BodyEncoding = System.Text.Encoding.UTF8;
            // text or html
            myMail.IsBodyHtml = true;

            mySmtpClient.Send(myMail);
            return true;
        }
        catch (SmtpException ex)
        {
            return false;
        }
        catch (Exception ex)
        {
            return false;
        }



Any Ideas????????????

The Yahoo SMTP port is 465, not 25.

You also need EnableSsl set to true on the SmtpClient object.