5.7.57 SMTP - Client was not authenticated to send anonymous mail during MAIL FROM error

C#SmtpTimeout

C# Problem Overview


I have to send mails using my web application. Given the below code showing 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.

Help me to find a proper solution. Thank you.

Code:

protected void btnsubmit_Click(object sender, EventArgs e)
 {
  
   Ticket_MailTableAdapters.tbl_TicketTableAdapter tc;
   tc = new Ticket_MailTableAdapters.tbl_TicketTableAdapter();
   DataTable dt = new DataTable();
   dt = tc.GetEmail(dpl_cate.SelectedValue);
   foreach (DataRow row in dt.Rows)
    {
    string eml = (row["Emp_Email"].ToString());
    var fromAddress = "emailAddress";
    var toAddress = eml;
    const string fromPassword = "*****";
    string body = "Welcome..";
 // smtp settings
    var smtp = new System.Net.Mail.SmtpClient();
       {
         smtp.Host = "smtp.office365.com";
         smtp.Port = 587;
         smtp.EnableSsl = true;
         
         smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
         smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
         smtp.UseDefaultCredentials = false;
         smtp.Timeout = 600000;
       }
  // Passing values to smtp object
     smtp.Send(fromAddress, toAddress, subject, body);
     }
  } 
 }

C# Solutions


Solution 1 - C#

@Reshma- In case you have not figured it yet, here are below things that I tried and it solved the same issue.

  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.

Hope it helps.

Solution 2 - C#

You seem to be passing the From address as emailAddress, which is not a proper email address. For Office365 the From needs to be a real address on the Office365 system.

You can validate that if you hardcode your email address as the From and your Office 365 password.

Don't leave it there though of course.

Solution 3 - C#

I spent way too much time on this and the solution was super simple. I had to use my "MX" as the host and port 25.

        var sClient = new SmtpClient("domain-com.mail.protection.outlook.com");
        var message = new MailMessage();

        sClient.Port = 25;
        sClient.EnableSsl = true;
        sClient.Credentials = new NetworkCredential("user", "password");
        sClient.UseDefaultCredentials = false;
        
        message.Body = "Test";
        message.From = new MailAddress("[email protected]");
        message.Subject = "Test";
        message.CC.Add(new MailAddress("[email protected]"));

        sClient.Send(message);

Solution 4 - C#

I use to have the same problem.

Add the domain solved it..

mySmtpClient.Credentials = New System.Net.NetworkCredential("[email protected]", "password", "domain.com")

Solution 5 - C#

In my case, 2 Factor Authentication was turned on for the FROM account in Office 365. Once that was turned off, the email sent successfully.

Solution 6 - C#

Main two reasons only as mentioned in above comments

  1. NetworkCredentials you set should be correct. Verify with try actually signing into the account.
  2. You need to set UseDefaultCredentials to false first and then set Credentials Or Put smtp.UseDefaultCredentials = false; above the smtp.Credentials assignment.

Solution 7 - C#

This is an old question but since this is the first result in google for this error, I thought I would update my progress in this issue.

I spent way too may hours on this issue. In the end I had to change my Office 365 account's password few times until my code succeeded in sending emails.

Didn't have to make any changes in code.

Solution 8 - C#

In my case, I followed the following 3 steps and it worked.

If you are getting one of the following errors: 535 5.7.3 Authentication unsuccessful 5.7.57 Client not authenticated to send mail There are a few things you should check:

  1. Enable Client SMTP submission on the licensed mailbox being used: From Microsoft 365 Admin Center, go to Active Users and select the user. Go to the Mail tab. In the Email apps section, select Manage email apps. Verify that the Authenticated SMTP setting is checked (enabled). Select Save changes.

  2. Disable Multi-Factor Authentication (MFA) on the licensed mailbox being used: In the Microsoft 365 admin center, in the left navigation menu, choose Users > Active users. On the Active user's page, choose Multi-Factor Authentication. On the Multi-Factor Authentication page, select the user and disable the Multi-Factor Authentication status.

  3. Disable the Azure Security Defaults by toggling the Enable Security Defaults to No: Sign in to the Azure portal as a Security administrator, Conditional Access administrator, or Global administrator. Browse to Azure Active Directory > Properties. Select Manage security defaults. Set the Enable security defaults to toggle to No. Select Save.

Microsoft Reference Link

Solution 9 - C#

If you are using office 365 follow this steps:

  1. check the password expiration time using Azure power shell :Get-MsolUser -All | select DisplayName, LastPasswordChangeTimeStamp
  2. Change the password using a new password (not the old one). You can eventually go back to the old password but you need to change it twice.

Hope it helps!

Solution 10 - C#

In my case I was using the MailMessage constructor that takes two strings (to, from) and getting the same error. When I used the default constructor and then added a MailAddress object to the To property of the MailMessage it worked fine.

Solution 11 - C#

Probably the password of the account that you trying to send e-mail is expired. Just check your password policy expire date.

Solution 12 - C#

I changed the Office365 password and then tried to send a test email and it worked like a charm for me.

I used the front end (database mail option) and settings as smtp.office365.com port number 587 and checked the secure connection option. use basic authentication and store the credentials. Hope this turns out useful for someone.

Solution 13 - C#

If you reorder your code this way, it should work:

SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(mailOut, pswMailOut);
client.Port = 587; // 25 587
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;

MailMessage mail = new MailMessage();            
mail.From = new MailAddress(mailOut, displayNameMailOut);
mail.To.Add(new MailAddress(mailOfTestDestine));
mail.Subject = "A special subject";
mail.Body = sb.ToString();

client.Send(mail);

Solution 14 - C#

In my situation, our IT department made MFA mandatory for our domain. This means we can only use option 3 in this Microsoft article to send email. Option 3 involves setting up an SMTP relay using an Office365 Connector.

Solution 15 - C#

Try resetting your password for the email used. Had similar issue, and got it fixed only after changing password.

Solution 16 - C#

I had similar issue. please read this microsoft support article. specially this section. Outlook IMAP connection error

Solution 17 - C#

For us it failed this way but only from the servers (Azure) not from the developer machines.

It turned out that the Office365 admin required MFA when accessing office365 from outside of the corporate country.

Our production server was in Ireland but the dev machines where in Sweden -> confusion... The admin turned of MFA for our sending account -> works.

Solution 18 - C#

Started working after adding property:

mail.smtp.starttls.enable=true

Using:

mail.smtp.host=smtp.office365.com
mail.smtp.port=587
mail.transport.protocol=smtp
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.user[email protected]
mail.smtp.password=xxx
mail.smtp.from[email protected]

Solution 19 - C#

Set the User default credentials to true:

 smtp.UseDefaultCredentials = True;

Before that, input your credential:

smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);

This should work fine.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionReshmaView Question on Stackoverflow
Solution 1 - C#NavneetView Answer on Stackoverflow
Solution 2 - C#Joe VargasView Answer on Stackoverflow
Solution 3 - C#Ron RebennackView Answer on Stackoverflow
Solution 4 - C#Zvi RedlerView Answer on Stackoverflow
Solution 5 - C#DanielaView Answer on Stackoverflow
Solution 6 - C#AnkushView Answer on Stackoverflow
Solution 7 - C#SpitzView Answer on Stackoverflow
Solution 8 - C#Mohammad WaqasView Answer on Stackoverflow
Solution 9 - C#Matteo ContaView Answer on Stackoverflow
Solution 10 - C#horatius83View Answer on Stackoverflow
Solution 11 - C#bozokView Answer on Stackoverflow
Solution 12 - C#vozView Answer on Stackoverflow
Solution 13 - C#LucasetoView Answer on Stackoverflow
Solution 14 - C#carlin.scottView Answer on Stackoverflow
Solution 15 - C#VithuView Answer on Stackoverflow
Solution 16 - C#Vivek kumarView Answer on Stackoverflow
Solution 17 - C#Hans KarlsenView Answer on Stackoverflow
Solution 18 - C#Justinas JakavonisView Answer on Stackoverflow
Solution 19 - C#Freddy Jose Chirinos MorònView Answer on Stackoverflow