Not able to send emails from C# code using my outlook email.

Krishna Prasad 1 Reputation point
2022-01-12T02:44:02.033+00:00

I am a corporate employee.
as part of my work I need to send emails from my C# code.

with the below code I am able to send emails successfully using less secure gmail ID. But I am not able to do same with my corporate outlook exchange based email ID.
I get below exception.

"
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail. Error: 535 5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Mailbox. Visit https://aka.ms/smtp_auth_disabled for more information. [BN9PR03CA0659.namprd03.prod.outlook.com]"

what I should do ? is there a way I can make my corporate outlook exchange email ID less secure to enable to sending emails. (Like I did for gmail.)

below is the code I am using.

MailMessage message = new MailMessage(from, to);

        string mailbody = "In this article you will learn how to send a email using Asp.Net & C#";
        message.Subject = "Sending Email Using Asp.Net & C#";
        message.Body = mailbody;
        message.BodyEncoding = Encoding.UTF8;
        message.IsBodyHtml = true;         
        client.EnableSsl = true;
        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential("******@corporatecompany.com", "$password");            
        client.Host = "smtp.office365.com";
        client.Port = 587;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;          
        try
        {
            client.Send(message);
        }
Outlook | Windows | Classic Outlook for Windows | For business
Exchange | Exchange Server | Development
Exchange | Exchange Server | Management
{count} votes

2 answers

Sort by: Most helpful
  1. Glen Scales 4,446 Reputation points
    2022-01-17T22:56:20.837+00:00

    Instead of using SMTP you would be better to use the Graph API to send email from your code https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=csharp you want to try and avoid hard coding credentials in your code instead look at the client Client_Credentials oAuth flow https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow if you only need to send from one mailbox then you can scope these permission down to just that mailbox (or mailboxes) https://learn.microsoft.com/en-us/graph/auth-limit-mailbox-access which means you have a lot more secure application.

    2 people found this answer helpful.
    0 comments No comments

  2. Kael Yao 37,746 Reputation points Moderator
    2022-01-13T01:25:30.703+00:00

    Hi @Krishna Prasad

    Since your question is related to Exchange development, I have added the tag "office-exchange-server-dev" to it.
    Thanks for your understanding.


    According to the error message Error: 535 5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Mailbox., the cause of this issue may be the SMTP AUTH isn't enabled on this mailbox.
    Please refer to this link (point 1) to enable it: Fix issues with printers, scanners, and LOB applications that send email using Microsoft 365 or Office 365

    If you still get errors, please also make sure:

    1. Disable Multi-Factor Authentication (MFA) on the licensed mailbox that's being used
    2. Disable the Azure Security Defaults by toggling the Enable Security Defaults to No
    3. Exclude the user from a Conditional Access policy that blocks Legacy Authentication

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.