88

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);
     }
  } 
 }
Lewis86
  • 511
  • 6
  • 15
Reshma
  • 1,370
  • 7
  • 24
  • 38

21 Answers21

45

@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.

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Navneet
  • 451
  • 3
  • 2
  • 13
    That's it! I didn't know "UseDefaultCredentials" clears the Credentials. Thanks! – nwestfall Jul 21 '16 at 20:03
  • 1
    In my case, it doesn't matter which one goes first. As long as the "UseDefaultCredentials" is false. It works. I wonder why. – Weihui Guo Oct 31 '18 at 11:57
  • 1
    In my case, someone has accidentally turned MFA on in some cases (but not most). You can go to this page using the email account to see if MFA is enabled in these cases (also triggers when trying to send email): https://mysignins.microsoft.com/security-info – hamboy Jul 20 '23 at 02:34
38

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.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Joe Vargas
  • 412
  • 4
  • 3
20

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("test@test.com");
        message.Subject = "Test";
        message.CC.Add(new MailAddress("dude@good.com"));

        sClient.Send(message);
Ron Rebennack
  • 2,666
  • 1
  • 22
  • 17
  • 2
    This one is worked for me. check this Microsoft article https://support.office.com/en-us/article/How-to-set-up-a-multifunction-device-or-application-to-send-email-using-Office-365-69f58e99-c550-4274-ad18-c805d654b4c4?ui=en-US&rs=en-US&ad=US – wajira000 Mar 09 '17 at 09:22
  • 1
    This worked for me, but I had to then go to https://sender.office.com to delist my IP address – Simon Price Jan 30 '18 at 00:18
  • 4
    for me sClient.EnableSsl = true; was not defined. – Muhammad Ali Feb 25 '18 at 00:16
  • 8
    `You need to set "UseDefaultCredentials" to false first and then set Credentials. If you set "UseDefaultCredentials" after that it resets the NetworkCredential to null.` – Kiquenet May 09 '18 at 09:38
  • 4
    How you get the ***MX*** value ? – Kiquenet May 09 '18 at 09:47
  • 1
    Which is the difference about `domain-com.mail.protection.outlook.com` and `smtp.office365.com` ? Can I send to xxx@anotherDomain recipients ? – Kiquenet May 09 '18 at 09:58
  • 2
    Using the MX record as host enables you to send email only to internal email addresses (ie only to the same domain as the sending address) – Lez Dec 17 '18 at 15:17
  • 1
    @Kiquenet You may need to ask your system administrator. More info here: https://learn.microsoft.com/en-us/exchange/mail-flow-best-practices/how-to-set-up-a-multifunction-device-or-application-to-send-email-using-microsoft-365-or-office-365#step-by-step-instructions-for-direct-send – Martijn Oct 08 '20 at 07:07
18

I use to have the same problem.

Add the domain solved it..

mySmtpClient.Credentials = New System.Net.NetworkCredential("email@domain.com", "password", "domain.com")
Zvi Redler
  • 1,708
  • 1
  • 18
  • 29
  • 3
    This is the correct solution for me. I've had an application running, sending daily emails for over a year, suddenly it stopped working. The change to fix it is this one, even though my network credentials aren't actually my email, this fix worked. – EJA Nov 29 '16 at 15:10
  • 5
    It's not adding the domain name, but using the email address as the userName solved my problem. `NetworkCredential(email, password);` – Weihui Guo Nov 01 '18 at 13:19
13

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.

Daniela
  • 657
  • 1
  • 10
  • 25
13

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

Mohammad Waqas
  • 167
  • 2
  • 8
  • 1
    Step 1 worked for me. I had Authenticated SMTP unchecked. When checked and saved, emails started working through my CSharp program. – Chirag May 29 '22 at 16:37
12

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.
Chitrank Dixit
  • 3,961
  • 4
  • 39
  • 59
Ankush
  • 121
  • 1
  • 6
7

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.

Spitz
  • 713
  • 1
  • 9
  • 17
5

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!

Matteo Conta
  • 1,423
  • 13
  • 17
4

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

bozok
  • 51
  • 2
3

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.

horatius83
  • 393
  • 2
  • 8
  • Finally a working answer for me. Thanks! I was using a simple string for the To field instead of type MailAddress. The error message is definitely misleading – West May 17 '21 at 08:26
3

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);
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Lucaseto
  • 51
  • 2
2

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.

voz
  • 21
  • 1
2

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.

carlin.scott
  • 6,214
  • 3
  • 30
  • 35
2

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

Vithu
  • 141
  • 1
  • 8
2

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

Vivek kumar
  • 239
  • 3
  • 7
1

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.

Hans Karlsen
  • 2,275
  • 1
  • 15
  • 15
1

In my case the problem was solved by dependency update 1.4 -> 1.4.7

<dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
</dependency>
bausov
  • 387
  • 3
  • 12
1

In my case, I could compile the program and run it from our server and it ran without issue. When trying to run the program from the VS IDE I would get the same error as the OP. To resolve, I had to add code to detect that I was running the code from the IDE. For my particular situation, when running from the IDE, I needed to turn off SSL. I also wrapped the client in a using statement for easier disposal:

  using (SmtpClient smtp = new SmtpClient())
  {
      if (System.Diagnostics.Debugger.IsAttached)
      {
          // We are running this from the IDE
          //smtp.EnableSsl = true;
          smtp.UseDefaultCredentials = false;
          smtp.Credentials = new System.Net.NetworkCredential(user, pwd);
          smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
       }
       smtp.Send(message);
   }
OldDog
  • 334
  • 4
  • 12
-1

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=xxx@example.com
mail.smtp.password=xxx
mail.smtp.from=yyy@example.com
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114
-3

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.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47