I've got a simple method that is supposed to send emails below but I can't get it to work.
private void sendEmail(string mailto, string mailfrom, string subject, string body)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(Properties.Settings.Default.smtpServer);
mail.From = new MailAddress(Properties.Settings.Default.mailFrom);
mail.To.Add(Properties.Settings.Default.emailTo);
mail.Subject = subject;
mail.Body = body;
SmtpServer.EnableSsl = true;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Port = Properties.Settings.Default.smtpPort;
SmtpServer.Credentials = new System.Net.NetworkCredential(Properties.Settings.Default.smtpServer, Properties.Settings.Default.emailPassword);
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.Timeout = 200000;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
logAndLoad(ex.Message.ToString(), "sendEmail");
}
}
I have checked the credentials, port, server ect at runtime and everything checks out but I'm getting the below:
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
I am trying to use my personal gmail account so I'm using that as the mailfrom address and also as part of the credentials. The password is 100% correct, and I'm trying to use port 587 and an smtp server of smtp.gmail.com
Any ideas would be much appreciated.
The question is different from the suggested duplicate as I've tried the steps there and I'm already using System.Net.Mail
Please see revised code, it matches the suggestions in the other question and still fails.