1

I'm receiving an exception when trying 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 my Gmail account, I allowed IMAP and POP in the settings tab.

What I've tried:

  • Changing the port to 587 and 25. this time I'm 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
Akif
  • 7,098
  • 7
  • 27
  • 53
Ori Refael
  • 2,888
  • 3
  • 37
  • 68

1 Answers1

6

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.

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • if i haven't noticed my code, the code you provided is exactly the same as mine. And yes, before i posted this question i also read those answers, no help with that. Though i haven't linked any ip with gmail, just set the settings i've mentioned – Ori Refael Mar 21 '16 at 09:39
  • have you tried by specifying credentials and Turn On Access for less secure apps in the gmail account? – Niranjan Singh Mar 21 '16 at 10:00
  • i coudln't find the section where i can choose what kind of access i allow to the accout. i only activated IMAP and POP, according to the answer in the topic you gave me, it's no longer where they say it is – Ori Refael Mar 21 '16 at 10:10
  • Ok, i found where. please update your answer with the link : https://support.google.com/accounts/answer/6010255?hl=en also the port should be 587 after that. ill accept this answer – Ori Refael Mar 21 '16 at 10:20
  • @OriRefael - I have updated port but i am still working with port 465 and already enabled setting in my google account. it is still working fine.. It's good that you have solved your issue. – Niranjan Singh Mar 21 '16 at 10:29
  • well, if you find anything with 465, keep me posted, its more secure i think – Ori Refael Mar 21 '16 at 16:01
  • 2
    Am I correct that that code is setting `smtpClient.Credentials` twice? If that is necessary then it is sure bizarre. – Sam Hobbs Sep 09 '19 at 19:31