0

When I m trying to send mail through my C# Code above error is showing. I have searched for possibly all solution but I do not get the solution. I am using the below Code:

MailMessage mail = new MailMessage("SenderMail",Email);
        mail.IsBodyHtml = true;
        mail.Subject = "An email from Office365";
        mail.Body = "<html><body><h1>Hello world</h1></body></html>";
        SmtpClient client = new SmtpClient("smtp.office365.com");
        client.Port = 587;
        client.EnableSsl = true;
        client.UseDefaultCredentials = false; // Important: This line of code must be executed before setting the NetworkCredentials object, otherwise the setting will be reset (a bug in .NET)
        NetworkCredential cred = new System.Net.NetworkCredential("Sendermail", "Password");
        client.Credentials = cred;
        client.Send(mail);
tereško
  • 58,060
  • 25
  • 98
  • 150
rahul ladwal
  • 11
  • 1
  • 2
  • 3
  • 1
    Are you 100% certain the credentials (user: Sendmail, Password: Password) is correct, including capitalisation? – Neil Apr 20 '17 at 08:12
  • Yes Neil I Cross Checked it by logging in my Account @Neil – rahul ladwal Apr 20 '17 at 08:13
  • 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 [MA1PR01CA0093.INDPRD01.PROD.OUTLOOK.COM] – rahul ladwal Apr 20 '17 at 08:20
  • try putting the full email address in the sender credentials (e.g. sendermail@office365.com) – Neil Apr 20 '17 at 08:42
  • it's just a sample. I have used a full email address – rahul ladwal Apr 20 '17 at 09:30
  • Please verify your credentials. if it is wrong we will get the same error message.. – Nijin P J Aug 19 '20 at 04:27

3 Answers3

2

1- I believe the following is missing from your code.

client.DeliveryMethod = SmtpDeliveryMethod.Network;

2- Dont set UseDefaultCredentials value to false. Keep it to its default value. Just remove the line.

This worked for me.

Houssam Hamdan
  • 888
  • 6
  • 15
0

If you haven't, then I believe you may need to enable client sending from your Office365 account. See this page for details.

Neil
  • 11,059
  • 3
  • 31
  • 56
  • I have read this already and in my coding you can see i have done all the things mentioned there. Is there an setting like gmail where we allow access to third party application to send emails. – rahul ladwal Apr 20 '17 at 08:26
0

Context: I faced the same error, and I thought my credentials were good, because it was working okay every day. I tried to debug, looked for a solution in internet (even in this stackoverflow question-thread), and I spent lots of minutes with no good result.

Solution: Finally, I tried my "supposed correct" credentials on outlook web client and logged in. Result: I got this message: "you password expired! " then I updated the password in web client, I updated credentials and my code and that's it, it worked okay as usual.

Additional info: You may consider to keep password (encrypted) somewhere else (not in source code), so that it can be easily updated.

WalterAgile
  • 191
  • 1
  • 4