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, theDeliveryMethod
properties - commenting/uncommenting the
IsBodyHtml
andBodyEncoding
properties