SMTP unrecognized authentication type with Office 365

I am getting this error and have tried so many different options. Using Office 365

Error: 504 5.7.4 Unrecognized authentication type [MN2PR20CA0010.namprd20.prod.outlook.com]

Settings here:

  DISCOURSE_SMTP_ADDRESS: smtp.office365.com
  DISCOURSE_SMTP_PORT: 587
  DISCOURSE_SMTP_USER_NAME: email
  DISCOURSE_SMTP_PASSWORD: "PASSWORD"
  DISCOURSE_SMTP_ENABLE_START_TLS: true

try your configuration using openssl tool first

  1. Encode your office365 user
echo -n "your email here " | openssl enc -base64
xxx_encoded_email_xxx

2.Encode your password

echo -n "your password here" | openssl enc -base64
xxx_encoded_password_xxx

3.Open a connection

openssl s_client -connect SMTP.office365.com:587 -starttls smtp -quiet -crlf

4.Test authentification

  • wait for the message 250 SMTPUTF8
  • send EHLO SMTP.office365.com
  • wait for response
    250-PR3P189CA0029.outlook.office365.com Hello [88.138.0.68]
    250-SIZE 157286400
    250-PIPELINING
    250-DSN
    250-ENHANCEDSTATUSCODES
    250-AUTH LOGIN XOAUTH2
    250-8BITMIME
    250-BINARYMIME
    250-CHUNKING
    250 SMTPUTF8
    
  • send AUTH LOGIN
  • wait for 334 VXNlcm5hbWU6
  • send your encoded user login xxx_encoded_email_xxx
  • wait for 334 UGFzc3dvcmQ6
  • send your encoded password xxx_encoded_password_xxx
  • wait for response 235 2.7.0 Authentication successful
2 Likes

So if the openssl test works but the connection test run by discourse-doctor still fails when using those settings what is next? Even though the openssl test works my discourse connection still fails with 504 5.7.4 Unrecognized authentication type… Presumably it is not recognizing the LOGIN auth method. I tried adding DISCOURSE_SMTP_AUTHENTICATION: login to the app.yml file, but that has not helped.

FWIW, here is what I have found. I went into the docker image and started monkeying around with the lib/tasks/emails.rake script and the config/discourse.conf file. Several hours later, after trying every possible combination of smtp.office365.com and .mail.protection.outlook.com AUTH login and AUTH plain I uncommented the block above the STMP start.

# We would like to do this, but Net::SMTP errors out using starttls
#Net::SMTP.start(smtp[:address], smtp[:port]) do |s|
#  s.starttls if !!smtp[:enable_starttls_auto] && s.capable_starttls?
#  s.auth_login(smtp[:user_name], smtp[:password])
#end

Running that as-is resulted in a read timeout.
Modifying it like this - calling new rather than start resulted in a successful send.

Net::STMP.new(smtp[:address], smtp[:port]) do |s|
    s.enable_starttls
    s.auth_login(smtp[:user_name], smtp[:password])
end

This is my first exposure to Ruby, rake and friends. I can’t explain why it works or if is a ‘good thing’ for general cases.
J.

Oh, also I never was able to get it to work to the my_domain.mail.protection.outlook.com on either 25 or 587, what worked was smtp.office365.com:587.

1 Like

I use socketlabs.com as my email delivery service, and had a similar issue. In my case the solution was to edit lib/tasks/emails.rake as follows:
Change the line:
Net::SMTP.start(smtp[:address], smtp[:port], 'localhost', smtp[:user_name], smtp[:password])
to
Net::SMTP.start(smtp[:address], smtp[:port], 'localhost', smtp[:user_name], smtp[:password], smtp[:authentication])

Without this change, the DISCOURSE_SMTP_AUTHENTICATION: login doesn’t get passed to the lower level SMTP code.

I haven’t tested this to see if the modified code still works for other authentication methods, but it fixes the problem for login authentication.

3 Likes

I can confirm, that adding
smtp[:authentication]
to Net::SMTP.start call and setting
DISCOURSE_SMTP_AUTHENTICATION: login
in app.yml fixes the issue for email test page.

I believe, that regular emails are send via mail library, and setting
DISCOURSE_SMTP_AUTHENTICATION: login
in app.yml is enough for the library to work correctly.

Still, I believe, that lib/tasks/emails.rake should be patched to use DISCOURSE_SMTP_AUTHENTICATION setting. This would save some unnecessary debugging .

1 Like

Looks like a :bug: to me. I submitted a PR:

https://github.com/discourse/discourse/pull/10572

3 Likes

Hi,

Net::SMTP.start(smtp[:address], smtp[:port], ‘localhost’, smtp[:user_name], smtp[:password], smtp[:authentication])
rescue Exception => e

Has been patched as above in my current version but still get the same.

Error: 504 5.7.4 Unrecognized authentication type with smtp.office365.com.

Anyone have any ideas?

1 Like

How do I edit the lib/tasks/emails.rake script to get this to work?

As a general update on this issue, MS are in the process of removing legacy authentication for SMTP and POP3 which is going to make things difficult if you are using O365 as a mail provider. The default policy applied now is to prohibit SMTP AUTH and you need to enable this per-mailbox. Hope that’s useful — I spent yesterday morning banging my head against this wall.

It’s a real shame about POP in particular, because setting up inbound mail is going to get a lot harder unless Discourse adds IMAP support for inbound mailboxes.

1 Like

There’s an easy solution for incoming mail that includes a container to receive it. It used to be called "straightforward "but someone objected to the name and it was changed and I can’t find it anymore.

There is also IMAP support for group inboxes. But it mostly supports Gmail only.

3 Likes

Well, running your own mail server should be considered a method of last resort in any case. I believe it was “direct delivery”, try searching for that.

2 Likes

As of 2023, pop3 auth is not possible anymore in exchange.

1 Like

If you are using PHPMailer
Then try by comments/remove the line and try : //$mail->isSMTP(true);