2
votes

I am using C# to send an email from my local computer, but I'm running into an error while executing the code below (for testing purposes).

C# code:

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

    MailMessage mail = new MailMessage()
    {
        To = { new MailAddress(emailAddres) },
        From = new MailAddress("[email protected]", "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;
   }
}

In the Web.config I added the below:

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network host="smtp.office365.com" port="587" defaultCredentials="false" enableSsl="true"  
                 userName="[email protected]" password="ThisPwd" targetName="STARTTLS/smtp.office365.com"
        />
      </smtp>
    </mailSettings>
  </system.net>

Error message:

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 [AM0PR05CA0077.eurprd05.prod.outlook.com] at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at...

Edit:

This could be a duplicate of SMTP 5.7.57 error when trying to send email via Office 365 But I couldn't find a solution there, so I'd like to reopen the discussion.

1
I have got the same issue on my tenant.Eugene Astafiev
In the meantime I found that we need to use the MX record. siteX.mail.protection.outlook.com, but I've not yet found a way to implement it. answers.microsoft.com/en-us/msoffice/forum/… .. For some reason this MX record is not accessible on port 587, and when I configure port 25 it is able to establish a connection, but then Microsoft blocked my public IP from sending emails.Dieter
Still haven't found a solution. I'm receiving the same error when the website is deployed on an Azure app service with https only enabled and on the free app service plan.Dieter
MX record is not needed. Check answer below.Dieter

1 Answers

1
votes

The code in the question is OK. It was basically configuration on the Office365 account that needed to be done.

Check the following documentation by Microsoft for more information.

https://docs.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission#enable-smtp-auth-for-specific-mailboxes

If you've bought an Office365 email account with GoDaddy, ask them to enable SMTP AUTH. They'll enable it for you, but you still won't be able to use the Office365 smtp server on their hosting unless you're using a Virtual Private Server as GoDaddy blocks port 587 for shared hosting.