Where to specify MX record value when sending an email from C# ASP.NET code?

In the below question (link) it was stated that the MX record needs to be used for sending emails from an ASP application.

https://answers.microsoft.com/en-us/msoffice/forum/msoffice_outlook-mso_winother/client-was-not-authenticated-to-send-anonymous/d405bcb0-f40c-42fa-b1b2-477597100123

[aamirghanchi] also confirmed that it worked for him after using the MX record instead of the ‘smtp.office365 .com’ host. (space added because new user is only allowed to post 2 URLs)

When using the MX record in the code in the web.config file:

<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network host="domainx-be.mail.protection.outlook.com" port="587" defaultCredentials="false" 
                 enableSsl="true" userName="*** Email address is removed for privacy ***" 
                 password="ThisPwd" targetName="STARTTLS/domainx-be.mail.protection.outlook.com"
        />
      </smtp>
    </mailSettings>
  </system.net>

the result is:

System.Net.Mail.SmtpException: Failure sending mail. —> System.Net.WebException: Unable to connect to the remote server —> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 104.47.17.138:587

When using ’ smtp.office365 .com ’ in the code in the web.config file:

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
          <network host="smtp.office365.com" port="587" defaultCredentials="false" 
                 enableSsl="true" userName="*** Email address is removed for privacy ***" 
                 password="ThisPwd" targetName="STARTTLS/smtp.office365.com" />
      </smtp>
    </mailSettings>
  </system.net>

the result is:

System.Net.Mail.SmtpException: 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 [AM0PR06CA0085.eurprd06.prod.outlook .com]

I’ve tested the connection via the test-netconnection function in PowerShell, it seems that the MX record is not accessible on port 587, but it seems to be accessible through port 25.

…BUT when I specified port 25 in my web.config and tried sending an email from my website Microsoft blocked my public IP…

Image

As I don’t want that to happen again, what should I do to get this to work on port 587 please?

This is the C# code that I use to execute the SendEmail:

protected void SendEmail(string emailAddres)
{
    SmtpClient smtp = new SmtpClient();

    MailMessage mail = new MailMessage()
    {
        To = { new MailAddress(emailAddres) },
        From = new MailAddress("email@email.com", "fromEmail"),
        Subject = "Subject of a test email!",
        IsBodyHtml = true,
        Body = "Test body email.",
        BodyEncoding = System.Text.Encoding.UTF8,
        SubjectEncoding = System.Text.Encoding.UTF8
    };

   try
   {
       smtp.Send(mail);
       lblResultEmail.Text = "Message Sent Succesfully";
       lblResultEmail.ForeColor = System.Drawing.Color.Green;
   }
   catch (Exception ex)
   {
       lblResultEmail.Text = ex.ToString();
       lblResultEmail.ForeColor = System.Drawing.Color.Red;
   }
}

PS. I’m not just running into this error on my local machine, but also on my Azure app service that has https only enabled on a free app service plan.

Thank you for your help!