为什么在 asp.net 中出现 SMTP 错误

why getting SMTP error in asp.net

我已经登录 page.login 工作正常,但在尝试创建新用户时,我得到

Error:The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

这是我用来发送邮件的代码

 public void SendConfirmationAfterRegister(String EmailID, String UserName)
    {
        MailMessage mailMsg = new MailMessage();

        String BodyMsg = UserName + ", \r\n\nWe have recieved your request to become a user of our site.  Upon review, we will send you verification for site access.\r\n\n" +
            "Thank you, \r\n\nMuda Admin";
        try
        {
            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential(ConfigurationManager.AppSettings["AdminEmail"].ToString(), ConfigurationManager.AppSettings["Pwd"].ToString()),
                EnableSsl = true
            };

            mailMsg.IsBodyHtml = true;
            client.Send(ConfigurationManager.AppSettings["AdminEmail"].ToString(), EmailID, "Received Your Request", BodyMsg);

            client.Send(ConfigurationManager.AppSettings["AdminEmail"].ToString(), "mymailId", "User Needs Access", EmailID + " looking for access the xyz Network Site.");
        }
        catch (Exception ex) { throw new Exception(ex.Message); }
    }

谁能告诉我有什么问题

这样试试...

using System.Net.Mail;

 String BodyMsg = UserName + ", \r\n\nWe have recieved your request to become a user of our site.  Upon review, we will send you verification for site access.\r\n\n" +
        "Thank you, \r\n\nMuda Admin";
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress(ConfigurationManager.AppSettings["AdminEmail"].ToString());
            mail.To.Add("To@domain.com");
            mail.Subject = "subject";
            mail.Body = BodyMsg ;

            SmtpServer.UseDefaultCredentials=true
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["AdminEmail"].ToString(), ConfigurationManager.AppSettings["Pwd"].ToString());
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);

试试这个:

public void SendConfirmationAfterRegister(String EmailID, String UserName)
{
    MailMessage mailMsg = new MailMessage();

    String BodyMsg = UserName + ", \r\n\nWe have received your request to become a user of our site.  Upon review, we will send you verification for site access.\r\n\n" +
        "Thank you, \r\n\nMuda Admin";
    try
    {
        using (var client = new SmtpClient("smtp.gmail.com", 587))
        {
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["AdminEmail"].ToString(), ConfigurationManager.AppSettings["Pwd"].ToString());
            client.EnableSsl = true;

            mailMsg.IsBodyHtml = true;
            client.Send(ConfigurationManager.AppSettings["AdminEmail"].ToString(), EmailID, "Received Your Request", BodyMsg);

            client.Send(ConfigurationManager.AppSettings["AdminEmail"].ToString(), "mymailId", "User Needs Access", EmailID + " looking for access the xyz Network Site.");
        }
    }
    catch (Exception ex) { throw new Exception(ex.Message); }
}

您需要将 UseDefaultCredentials 设置为 false

注意:您必须在 设置 Credentials 属性.

之前将 UseDefaultCredentials 设置为 false