无法通过yahoo,gmail smtp服务器从localhost发送邮件

时间:2013-12-30 12:48:34

标签: c# asp.net smtp gmail smtpclient

从我的localhost网络应用程序发送测试邮件时,下面提到的asp.net代码有什么问题?

  

错误:SMTP服务器需要安全连接或客户端   未经过身份验证。服务器响应为:5.7.1身份验证   需要

        string smtpAddress = "smtp.mail.yahoo.com";
        int portNumber = 587;
        bool enableSSL = true;

        string emailFrom = "abcdefg@gmail.com";
        string password = "12345";
        string emailTo = "zyxw@gmail.com";
        string subject = "Hello";
        string body = "Hello, I'm just writing this to say Hi!";

        using (MailMessage mail = new MailMessage())
        {
            mail.From = new MailAddress(emailFrom);
            mail.To.Add(emailTo);
            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = true;
            // Can set to false, if you are sending pure text.


            using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
            {
                smtp.UseDefaultCredentials = false; 
                smtp.Credentials = new NetworkCredential(emailFrom, password);
                smtp.EnableSsl = enableSSL;
                smtp.Send(mail);
            }
        }

1 个答案:

答案 0 :(得分:5)

服务器需要身份验证。雅虎不只是为任何人发送电子邮件。我认为您不能使用Google帐户通过其网关发送电子邮件。