SMTP with office365?

Hello

I try to use Office365 as SMTP for discourse. I’m already using this account for my wordpress SMTP so the account is not the problem.

When i try to send a email test, an error message appear

[Sender] 504 5.7.4 Unrecognized authentication type [AM3PR05CA0123.eurprd05.prod.outlook.com]

This is my SMTP config from app.yml

## TODO: The SMTP mail server used to validate new accounts and send notifications
DISCOURSE_SMTP_ADDRESS: smtp.office365.com
DISCOURSE_SMTP_PORT: 587
DISCOURSE_SMTP_USER_NAME: test@example.fr
DISCOURSE_SMTP_PASSWORD: password
DISCOURSE_SMTP_AUTHENTICATION: login
DISCOURSE_SMTP_ENABLE_START_TLS: false 

Any idea how to fix the problem ?

Another config:

## TODO: The SMTP mail server used to validate new accounts and send notifications
DISCOURSE_SMTP_ADDRESS: smtp.office365.com
DISCOURSE_SMTP_PORT: 587
DISCOURSE_SMTP_USER_NAME: test@example.fr
DISCOURSE_SMTP_PASSWORD: password
DISCOURSE_SMTP_AUTHENTICATION: login
DISCOURSE_SMTP_ENABLE_START_TLS: true

I get the following error:

550 5.7.60 SMTP; Client does not have permissions to send as this sender

The problem is from Discourse, because I can test with swaks and it’s working

swaks --to test@example.com --from test2@example.com --server smtp.office365.com --auth login --auth-user test2@example.com -tls

Not Suggesting that You use office365 for SMTP but it seems like DISCOURSE_SMTP_AUTHENTICATION: login isn’t required … maybe try commenting that out and try again with STARTTLS set to True?

Okay the problem is now fixed. Huge mistake from my side : the email from Admin → settings → notification email was not the same from the smtp username.
Maybe a check should be made to prevent user to makes this mistake :face_with_raised_eyebrow:

but it seems like DISCOURSE_SMTP_AUTHENTICATION: login isn’t required

True, the final config

DISCOURSE_SMTP_ADDRESS: smtp.office365.com
DISCOURSE_SMTP_PORT: 587
DISCOURSE_SMTP_USER_NAME: test@example.fr
DISCOURSE_SMTP_PASSWORD: password
DISCOURSE_SMTP_AUTHENTICATION: login
DISCOURSE_SMTP_ENABLE_START_TLS: true

Discourse cannot guess what addresses the mail server will deliver for.

Sadly this solution is not working for me (I also tried to escape the password). I tried it with this SMTP setting problem with Office365 - #5 by ianwhite one which is working. But not everybody gets E-Mails… :thinking:

If some people get mail then it’s the fault of office365, or their mail provider. The others should check their spam folder, though if they use a service life office365 it will silently drop mail that it thinks is spam.

I tried it with these settings. In the skipped section in the email tab I can see the following error:

554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied;

Sounds like the address you are sending from is not one that the mail server supports. There is a line at the bottom of app.yml that week let you set the notification address. (you can set it in sure settings if you’re logged in)

Or maybe they have VERP enabled so the email address is replies+{key}@example.com instead of the authorized noreply@example.com That was the issue I’ve faced in past while trying to send mails from my own mail server. It’s usually easy to fix though.

はい、これで私の環境では動作しました。初期設定で以下の2点を同じメールアドレスに設定する必要があります。

DISCOURSE_SMTP_USER_NAME:
-- AND --
DISCOURSE_NOTIFICATION_EMAIL:

Office365とダイレクトデリバリーで苦労している方への最新の手順です。

  • Microsoft管理センターに移動します。
  • [設定]、[ドメイン]の順にクリックします。
  • [ドメインの追加]をクリックします。
  • 画面の指示に従って、フォーラムのURL(例:forum:yourlinks.com)を追加します。
  • 上記の手順を完了し、DNSが検証されたら、そのサブドメインを使用して受信トレイを作成できるようになります。
  • [ユーザー]、[アクティブなユーザー]の順にクリックします。
  • [ユーザーの追加]をクリックします。
  • フォーラムのメールボックスを作成します(例:noreply@forum.yourlinks.com)。メールボックスのドメインは、フォーラムが実行されているホスト名と一致する必要があります。
  • [ライセンスとアプリ]をクリックします。
  • このメールボックスにExchange Online (Plan 1) ライセンスを割り当てます(1日10,000通、毎分30通のスロットル)。ダイレクトデリバリーの目的でメールボックスにライセンスを添付する必要があります。執筆時点での費用は月額$4.00USDまたは$5.10CADです。より高い制限または短いスロットルが必要な場合は、他のプランも利用可能です。
  • [メール]、[メールアプリの管理]の順にクリックします。
  • [認証済みSMTP]を有効にします。
  • [変更の保存]をクリックします。
  • Azureポータルに移動します。
  • アクティブディレクトリに移動します。
  • 左側のサイドバーで、[ユーザー設定]のすぐ下にある[プロパティ]を選択します。
  • [セキュリティの既定値の管理]を選択します。
  • [セキュリティの既定値]を「無効」に設定します。
  • [保存]をクリックします。
  • [ユーザー] > [アクティブなユーザー]に戻ります。
  • [多要素認証]をクリックします。
  • 使用するメールボックスのMFAを無効にします。

これで、メールボックスはDiscourseで使用できるようになりました。

「セキュリティの既定値を「無効」に設定する」

これがSMTP認証を妨げていた唯一のことでした…
何日も頭を抱えていました…
なぜMSは物事をこんなにも難しくするのでしょうか…?

MFA の適用により、セキュリティの既定値の無効化は不要になる場合があります。M365 アカウントで MFA を有効にしてから適用すると、アカウントの MFA セットアップ ページに移動して、MFA をバイパスするために使用するアプリ パスワードを追加できます。MFA の適用、SMTP AUTH、およびセキュリティの既定値がすべて有効になっている M365 Business Basic ライセンスを持つユーザーで動作させることができました。SMTP のオープンおよび読み取りタイムアウトを 2 行追加して調整する必要があっただけです。30 は過剰かもしれませんが、5 は少なすぎました。

動作する app.yml エントリ(2023 年 9 月現在):

  DISCOURSE_SMTP_ADDRESS: smtp.office365.com
  DISCOURSE_SMTP_PORT: 587
  DISCOURSE_SMTP_USER_NAME: user@domain.com
  DISCOURSE_SMTP_PASSWORD: whatever-your-app-password-is
  DISCOURSE_SMTP_AUTHENTICATION: login
  DISCOURSE_SMTP_ENABLE_START_TLS: true
  DISCOURSE_NOTIFICATION_EMAIL: user@domain.com
  DISCOURSE_SMTP_OPEN_TIMEOUT: 30
  DISCOURSE_SMTP_READ_TIMEOUT: 30