【问题标题】:Send Mail with C# [duplicate]使用 C# 发送邮件 [重复]
【发布时间】:2012-05-01 00:05:12
【问题描述】:

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

我有这个例外:

SMTP 服务器需要安全连接,或者客户端不需要 认证。来自服务器的回复是:5.5.1 Authentication 必需的。了解更多信息,请访问

使用此代码:

    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);
    }

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

【问题讨论】:

标签: c# .net email


【解决方案1】:

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

This is a cleaner implementation of the same task

【讨论】:

    【解决方案2】:

    您输入的密码不正确,或者(更有可能)您在启用 Google 的 2-Step Authentication 后尝试使用普通密码

    您需要生成并使用application specific password

    【讨论】:

      【解决方案3】:

      这段代码对我有用

        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);
            }
      

      【讨论】:

        猜你喜欢
        • 2017-12-05
        • 2014-07-20
        • 2012-09-11
        • 2011-08-18
        • 2018-06-27
        • 1970-01-01
        • 2012-11-09
        • 1970-01-01
        • 2015-11-07
        相关资源
        最近更新 更多