1

I have a web app and i want to send an email within my app. But i face this error: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required. Here is my code sample:

 public static bool SendMail(string subject, string body, params string[] toMails)
    {
        try
        {
            var mailMsg = new MailMessage();
            mailMsg.BodyEncoding = Encoding.UTF8;
            mailMsg.HeadersEncoding = Encoding.UTF8;
            mailMsg.SubjectEncoding = Encoding.UTF8;
            mailMsg.Priority = MailPriority.High;
            mailMsg.Subject = subject;
            mailMsg.Body = body;
            mailMsg.IsBodyHtml = true;
            mailMsg.From = new MailAddress("mail@yahoo.com","",Encoding.UTF8);
            mailMsg.Sender = new MailAddress("mail@mail.com", "", Encoding.UTF8);
            foreach (var mail in toMails)
            {
                mailMsg.To.Add(new MailAddress(mail));
            }
            SmtpClient smtp = new SmtpClient();
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("mail@yahoo.com", "pass", "smtp.mail.yahoo.com");
            smtp.Host = "smtp.mail.yahoo.com";
            smtp.Port = 587; 
            smtp.UseDefaultCredentials = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Send(mailMsg);
            return true;
        }
        catch
        {
            return false;
        }
    }
Hossein Dahr
  • 75
  • 11
  • i have used this port too but i got this error "Failure sending mail." – Hossein Dahr Sep 27 '16 at 11:42
  • Try taking a look at this answer: http://stackoverflow.com/questions/18503333/the-smtp-server-requires-a-secure-connection-or-the-client-was-not-authenticated/27896975#27896975 – TryingToImprove Sep 27 '16 at 13:31
  • Possible duplicate of [C# SMTP fails to authenticate on Outlook.com, port 587. "The server response was: 5.7.1 Client was not authenticated"](http://stackoverflow.com/questions/14021649/c-sharp-smtp-fails-to-authenticate-on-outlook-com-port-587-the-server-respons) – Ruchi Sep 27 '16 at 14:59

2 Answers2

0

I think your issue is the port number being used. According to the below, port 465 for TLS not 587.

http://support.sliqtools.co.uk/kb/a32/smtp-email-settings-to-send-from-yahoo-email-addresses.aspx

Kitson88
  • 2,889
  • 5
  • 22
  • 37
0

Remove this line, if this is true its not using the credentials you define a few lines up.

    smtp.UseDefaultCredentials = true;
Daniel
  • 2,744
  • 1
  • 31
  • 41