Unable to send SMTP mails using office365 settings
Stefan Bogdanescu
Founder & Senior Architect
Solving the SMTP SendAsDeniedException with Office 365 in Laravel: A Developer's Guide
Sending emails reliably is a fundamental requirement for many applications, and when using services like Microsoft Office 365 via SMTP, developers often run into frustrating authentication hurdles. As a senior developer, I’ve seen countless similar issues arise, especially when dealing with corporate or restricted email accounts where permissions are tighter than expected.
If you are using Laravel to dispatch emails through the standard Office 365 SMTP settings and encountering the SendAsDeniedException, it signals that the problem lies not in your Laravel code itself, but in the security configuration or permissions of the Office 365 account being used.
This post will dissect why this error occurs, provide the specific troubleshooting steps required to resolve it, and guide you toward a robust solution.
Understanding the Error: SendAsDeniedException
The error message you are seeing—STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message—is a rejection from the mail server (in this case, Microsoft Exchange/Office 365). It means that while your Laravel application successfully connected to the SMTP server, the credentials provided were rejected by the server for authorization to send mail on behalf of that specific user account.
This is a permission issue, not a connection issue. The server knows who you are (authentication successful), but it denies the action (authorization failed).
Why Does This Happen with Office 365?
The discrepancy you observed—where testing with a separate Outlook account works perfectly, but your client email fails—is the key diagnostic clue. It strongly suggests that the specific permissions granted to your client email mailbox are stricter than those of a standard test account.
Here are the most common reasons this occurs in an Office 365/SMTP setup:
- MFA and App Passwords: If Multi-Factor Authentication (MFA) is enabled on the user's account, standard password authentication via SMTP might be blocked. Microsoft often requires the use of an App Password instead of the regular mailbox password for external application access.
- SMTP AUTH Restrictions: Security policies within the Microsoft 365 tenant may restrict SMTP AUTH (the protocol used to send mail) unless specific administrative settings are enabled.
- Delegation/Permissions: The user account you are attempting to log in with might lack the necessary permissions to act as a sender for external applications, especially if it is a shared or restricted client mailbox.
Practical Troubleshooting Steps
Since you have confirmed the connection details (smtp.office365.com, port 587, TLS) are correct, we must focus on adjusting the credentials and server configuration.
Step 1: Implement App Passwords (The Most Likely Fix)
If MFA is active for the user account, this is almost certainly the culprit. You need to generate an App Password specifically for that Office 365 user account within their Microsoft security settings. Use this generated password in place of your regular mailbox password when configuring your Laravel environment variables.
Step 2: Review Tenant Settings (Administrator Check)
If App Passwords do not resolve the issue, the restriction lies at the tenant level. An administrator must verify the following settings in the Microsoft 365 Admin Center or Exchange Admin Center:
- SMTP AUTH: Ensure SMTP AUTH is enabled for the user accounts in question.
- Conditional Access Policies: Check if any Conditional Access policies are blocking access to external SMTP services initiated by applications.
Step 3: Laravel Configuration Example
Ensure your environment configuration maps correctly to the account that has the necessary permissions. While this doesn't fix the server-side issue, it ensures Laravel is attempting the correct authentication flow.
// config/mail.php (Example structure)
'smtp' => [
'host' => 'smtp.office365.com',
'port' => 587,
'encryption' => 'tls',
'username' => env('MAIL_USERNAME'), // Use the restricted user/App Password here
'password' => env('MAIL_PASSWORD'), // Use the generated App Password here
'port' => 587,
],
Conclusion: Building Robust Email Infrastructure
Troubleshooting external services like Office 365 SMTP requires shifting perspective from application logic to security and authorization policies. The SendAsDeniedException is a classic indicator that the server is enforcing security boundaries.
By systematically checking for App Passwords and consulting with your IT administrator regarding SMTP AUTH permissions, you can resolve these frustrating send failures. Remember, building scalable applications—whether using frameworks like Laravel or other robust architectures—depends on anticipating these external security requirements. For further insights into developing solid backend systems, I highly recommend exploring the principles outlined at https://laravelcompany.com.
Note: Blog content is currently available in English.