1

尽管 Stackoverflow 上有关于此的所有主题。我无法找到这个问题的解决方案:

我有这个例外:

SMTP 服务器需要安全连接或客户端未通过身份验证。来自服务器的回复是:5.5.1 Authentication Required。了解更多信息

使用此代码:

    var fromAddress = new MailAddress(user.Email, "From Name");
    var toAddress = new MailAddress("myadress@gmail.com", "To Name");
    const string fromPassword = "fromPassword";
    const string subject = "Subject";
    const string body = "Body";

    var smtp = new SmtpClient
    {
        Host = "smtp.gmail.com",
        Port = 587,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
        Timeout = 20000
    };
    using (var message = new MailMessage(fromAddress, toAddress)
    {
        Subject = subject,
        Body = body
    })
    {
        smtp.Send(message);
    }

请问我错过了什么?感谢您的帮助

4

3 回答 3

1

这段代码对我有用

  SmtpClient sc = new SmtpClient("smtp.gmail.com");
  NetworkCredential nc = new NetworkCredential("username","password");
  //username doesn't include @gmail.com         
   sc.UseDefaultCredentials = false;
   sc.Credentials = nc;
   sc.EnableSsl = true;
   sc.Port = 587;
     try
      {
       sc.Send(mm);
      } 
     catch (Exception ex)
      {
        EventLog.WriteEntry("Error Sending", EventLogEntryType.Error);
      }
于 2013-05-13T07:46:20.357 回答
1

你没有错过任何东西。我怀疑你的密码错误。您没有忘记将其从“fromPassword”更改为真正应该的,对吗?

这是相同任务的更简洁的实现

于 2012-04-19T13:45:29.453 回答
1

Either you have typed your password incorrectly or (more likely) you are trying to use your normal password after you have enabled Google's 2-Step Authentication

You need to generate and use an application specific password

于 2013-05-13T09:41:48.827 回答