简体   繁体   English

ASP.NET C# - 从Gmail / Yahoo Live SMTP Server发送电子邮件不起作用

[英]ASP.NET C# - Sending Email from Gmail/Yahoo Live SMTP Server not working

I am struggling with attempting to send email from a .NET application. 我正在努力尝试从.NET应用程序发送电子邮件。

I have tried everything from making a new account both gmail and yahoo (changed the host for yahoo), as well as, changing the port using and not using mail.To, allowing less secure apps, i also tried enabling 2 step verification and giving an application password for gmail all of which have failed with the same caught error of: {"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required"} 我已经尝试了从创建一个新帐户gmail和yahoo(更改主机为yahoo),以及更改端口使用而不使用mail.To,允许不太安全的应用程序,我也尝试启用2步验证和给予gmail的应用程序密码,所有这些密码都失败并出现相同的捕获错误:{“SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.7.1需要身份验证”}

This is my code: 这是我的代码:

 SmtpClient smtpClient = new SmtpClient();
        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("myemail@gmail.com", "pw");
        smtpClient.Credentials = credentials;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Port = 587;
        smtpClient.EnableSsl = true;
        smtpClient.Host = "smtp.gmail.com";
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("myemail@gmail.com");
        mail.To.Add("myemail@gmail.com");
        mail.IsBodyHtml = true;
        mail.Subject = txtSubject.Text;
        mail.Body = txtBody.Text;
        try
        {
            smtpClient.Send(mail);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (mail != null)
            {
                mail.Dispose();
            }
        }

Solution: as people may have the problem in the future the solution was to remove the line of code: smtpClient.UseDefaultCredentials = false; 解决方案:由于人们将来可能遇到问题,解决方案是删除代码行:smtpClient.UseDefaultCredentials = false;

smtpClient.UseDefaultCredentials = false;            
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("myemail@gmail.com", "pw");
smtpClient.Credentials = credentials;

Use those lines in such order. 按顺序使用这些行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM