c#sendmail gmail smtp语法错误

时间:2016-03-21 09:05:46

标签: c# smtp

在执行此邮件发送方法时收到异常。

  

{"语法错误,命令无法识别。服务器响应是:"}

我的代码:

    body {
      background-color: white;
    }

    .parent {
      border: 1px solid black;
      width: 200px;
      height: 100px;
      position: relative;
      background-color: white;
    }

    .child {
      display: flex;
      justify-content: center;
      align-items: center;
      border: 0px;
      background-color: white;
      position: absolute;
      width: 150px;
      height: 103px;
      top: -1px;
      left: 25px
    }

在Gmail帐户中,我在设置标签中允许使用IMAP和POP。

我尝试过的事情:

  • 将端口更改为587和25.这次我得到 <div class="parent"> <div class="child"> Write your text here </div> </div>
  • 评论/取消注释public async static Task SendExceptionMail(Exception e) { try { //TODO: fill.. var message = new MailMessage(); message.To.Add("other_email_than_my_email@gmail.com"); message.From = new MailAddress("my_email_in_gmail@gmail.com"); message.Subject = "Server Exception Occured"; StringBuilder sb = new StringBuilder(); sb.AppendLine("Exception occured. Stack trace:"); sb.AppendLine(e.StackTrace); sb.AppendLine(""); sb.AppendLine("Time: " + DateTime.UtcNow); message.Body = sb.ToString(); message.IsBodyHtml = false; message.BodyEncoding = UTF8Encoding.UTF8; using (var smtpClient = new SmtpClient()) { smtpClient.Credentials = new System.Net.NetworkCredential("my_email_in_gmail@gmail.com", "very_very_complicated_password_with_numbers_and_signs"); smtpClient.Host = "smtp.gmail.com"; smtpClient.Port = 465; smtpClient.EnableSsl = true; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; //smtpClient.UseDefaultCredentials = false; await smtpClient.SendMailAsync(message); } } catch (Exception ex) { Console.Write(ex.StackTrace); } } 行,The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at属性
  • 评论/取消注释UseDefaultCredentialsDeliveryMethod属性

1 个答案:

答案 0 :(得分:2)

我建议你这样做 - Sending email in .NET through GmailCannot send mail from certain server

-First检查是否在Google帐户设置中启用了“Allowing less secure apps to access your account”,然后使用以下代码进行检查:

using (var smtpClient = new SmtpClient())
            {
                smtpClient.Credentials = new System.Net.NetworkCredential("my_email_in_gmail@gmail.com", "very_very_complicated_password_with_numbers_and_signs");
                smtpClient.Host = "smtp.gmail.com";
                smtpClient.Port = 587; // Google smtp port
                smtpClient.EnableSsl = true;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.UseDefaultCredentials = false;// disable it
                /// Now specify the credentials 
smtpClient.Credentials = new NetworkCredential(message.From.Address, fromPassword)

                await smtpClient.SendMailAsync(message);
            }

可能存在一些防火墙问题。

参考文献:
SendEmail in ASP.net shows me Syntax error, command unrecognized. The server response was: Dovecot ready
Syntax error, command unrecognized. The server response was ''

希望得到这个帮助。