c# sendmail gmail smtp syntax error

2019-09-03 20:39发布

问题:

im receiving an exception when tring to execute this mail sending method.

{"Syntax error, command unrecognized. The server response was: "}

my code:

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

In gmail account I allowed IMAP and POP in the settings tab.

What I've tried:

  • changing the port to 587 and 25. this time im getting 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
  • commenting/uncommenting the UseDefaultCredentials line, the DeliveryMethod properties
  • commenting/uncommenting the IsBodyHtml and BodyEncoding properties

回答1:

I suggest you to follow this - Sending email in .NET through Gmail and Cannot send mail from certain server

-First Check whether "Allowing less secure apps to access your account" is enable in google account setting then check with below code:

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

There may be some firewall issue .

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

Hope this help.



标签: c# smtp