none
SMTP server requires secure connection error when emailing within application RRS feed

  • Question

  • I am receiving the following error when attempting to send an email.   Am I doing something wrong, or might there be a setting on the server that is preventing success?

    The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

     smtp address = smtp.office365.com

     port = 587

    try
               {
                   string sSmtpAddress = Properties.Settings.Default.SmtpAdddress;
                   int iSmtpPort = Properties.Settings.Default.SmtpPort;
                   string sEmailToAddr = Properties.Settings.Default.EmailToAddr;
                   string sEmailFromAddr = Properties.Settings.Default.EmailFromAddr;
                   string sEmailFromPass = Properties.Settings.Default.EmailFromPass;
     
                   MailMessage mail = new MailMessage();
                   SmtpClient SmtpCl = new SmtpClient();
     
                   mail.From = new MailAddress(sEmailFromAddr);
                   mail.To.Add(sEmailToAddr);
                   mail.Subject = "Subject here";
                   mail.Body =  DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToShortTimeString() + " " + Message + " " + StackTrace;
                   SmtpCl.Host = sSmtpAddress;
                   SmtpCl.UseDefaultCredentials = false;
                   SmtpCl.Credentials = new System.Net.NetworkCredential(sEmailFromAddr, sEmailFromPass);
                   SmtpCl.Port = iSmtpPort;
                   SmtpCl.EnableSsl = true;               
                   SmtpCl.Send(mail);
               }
               catch (Exception ex)
               {
     
                   WriteTimeStampAndLog(ex.Message);
                   WriteLog(ex.StackTrace);
                   
               }
    

    Friday, March 9, 2018 9:44 AM

Answers

  • Hello Roblnstantiated,

    Try to refer the below link.

    The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

    1. Make sure that NetworkCredentials you set are correct. For example in my case since it was office SMTP, user id had to be used in the NetworkCredential along with domain name and not actual email id.
    2. You need to set "UseDefaultCredentials" to false first and then set Credentials. If you set "UseDefaultCredentials" after that it resets the NetworkCredential to null.

    Below is my testing code and you could refer with it.

      MailMessage MyMailMessage = new MailMessage();
    
      MyMailMessage.From = new MailAddress("xxxx@hotmail.com");
    
      MyMailMessage.To.Add("xxxx@microsoft.com");
      MyMailMessage.Subject = "Programmatic ";
      MyMailMessage.IsBodyHtml = true;
      MyMailMessage.Body = "<table><tr><td>" + "My First Dynamic Email" + "</table></tr></td>";
    
      SmtpClient SMTPServer = new SmtpClient("smtp.live.com");
    
      SMTPServer.Credentials = new NetworkCredential("xxxx@hotmail.com", "xxxxxxxxxxxxxxx");
      SMTPServer.EnableSsl = true;
      SMTPServer.Send(MyMailMessage);

    Note. hotmail server host is "smtp.live.com"

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 12, 2018 10:43 AM
    Moderator

All replies

  • Have you tried

    SmtpCl.UseDefaultCredentials = true;

    If that does not work try removing this line

    SmtpCl.EnableSsl = true;


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Friday, March 9, 2018 10:13 AM
    Moderator
  • Yes, I have tried that, unfortunately with the same results.

    Could there be some setting on the server side that needs to be addressed?

    Thanks

    Friday, March 9, 2018 10:17 AM
  • Yes, I have tried that, unfortunately with the same results.

    Could there be some setting on the server side that needs to be addressed?

    Thanks

    I modified my reply, also try removing

    SmtpCl.EnableSsl = true;


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 9, 2018 10:19 AM
    Moderator
  • So to be clear I made the following changes with the same result...

    SmtpCl.UseDefaultCredentials = true;
    // REMOVE SmtpCl.Credentials = new System.Net.NetworkCredential(sEmailFromAddr, sEmailFromPass);
    SmtpCl.Port = iSmtpPort;
    // REMOVE    SmtpCl.EnableSsl = true;               
    SmtpCl.Send(mail);


    Friday, March 9, 2018 10:37 AM
  • There is a chance this might be server side (have seen this happen for doing SMTP with Google mail) but I don't have any experience with this matter server side. 

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 9, 2018 11:52 AM
    Moderator
  • Yes I did test with Gmail and did run into an issue whereby I had to change a setting held against the account to "Allow Less secure Apps" before it would work. 

    Don't know if MS / Office 365 has any such settings? 

    Thanks for your input.


    Friday, March 9, 2018 12:17 PM
  • With GMail, there is a flag server side to update (https://myaccount.google.com/lesssecureapps?pli=1)

    Maybe it is the same thing...

    • Edited by Castorix31 Friday, March 9, 2018 12:19 PM
    Friday, March 9, 2018 12:18 PM
  • Hello Roblnstantiated,

    Try to refer the below link.

    The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

    1. Make sure that NetworkCredentials you set are correct. For example in my case since it was office SMTP, user id had to be used in the NetworkCredential along with domain name and not actual email id.
    2. You need to set "UseDefaultCredentials" to false first and then set Credentials. If you set "UseDefaultCredentials" after that it resets the NetworkCredential to null.

    Below is my testing code and you could refer with it.

      MailMessage MyMailMessage = new MailMessage();
    
      MyMailMessage.From = new MailAddress("xxxx@hotmail.com");
    
      MyMailMessage.To.Add("xxxx@microsoft.com");
      MyMailMessage.Subject = "Programmatic ";
      MyMailMessage.IsBodyHtml = true;
      MyMailMessage.Body = "<table><tr><td>" + "My First Dynamic Email" + "</table></tr></td>";
    
      SmtpClient SMTPServer = new SmtpClient("smtp.live.com");
    
      SMTPServer.Credentials = new NetworkCredential("xxxx@hotmail.com", "xxxxxxxxxxxxxxx");
      SMTPServer.EnableSsl = true;
      SMTPServer.Send(MyMailMessage);

    Note. hotmail server host is "smtp.live.com"

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 12, 2018 10:43 AM
    Moderator
  • This was finally resolved today when we tried a different smtp address.

    For whatever reason the address smtp.office365.com with port 587 did not work.

    We changed the smtp address and used port 25 and it solved the issue.

    Thanks to all for your responses!



     

    Tuesday, March 13, 2018 9:25 PM
  • Hello RobInstantiated,

    You could mark your reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Best regards,
    Neil Hu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 21, 2018 9:31 AM
    Moderator