Base solution for your next web application
Open Closed

Mailkit Exchange Online Gmail send mail #12590


User avatar
0
geoteam created

What is your product version? 14.1.0 What is your product type (Angular or MVC)? Angular What is product framework type (.net framework or .net core)? .NET 9.0

Hello, due to Microsoft's increased security settings (same like gmail for example), we are now experiencing problems sending emails from Aspnet Zero. Exchange Online requires 2FA, which means we can no longer use the previous method of sending emails via SMTP and an Office 365 user.

Since ASPNET Zero currently uses MailKit to send emails, the question now is how implementation via OAuth, Microsoft Graph API, or Azure Communication Services could be carried out without completely changing the ASPNET Zero code.

We are also open to alternatives that can be easily integrated into the existing structure, as this only concerns service emails from ASPNET Zero.

Many thanks in advance, Frank

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

3 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @geoteam

    Thank you for the detailed explanation. You are correct that basic SMTP authentication with Exchange Online is no longer viable due to mandatory MFA and security hardening. In ASP.NET Zero, however, this does not require a full rewrite of the email infrastructure.

    ASP.NET Zero abstracts email delivery via IEmailSender. MailKit is only the default implementation, and it can be replaced cleanly.

    This allows alternative providers or auth mechanisms to be integrated without touching core framework code.


    Microsoft Graph API (OAuth 2.0)

    This is the most future-proof approach if you stay within the Microsoft ecosystem.

    How it works

    • Register an Azure AD App
    • Grant Mail.Send (Application permission)
    • Use client credentials flow (no user, no MFA)
    • Send mail via POST /users/{sender}/sendMail

    ASP.NET Zero integration

    • Create a custom GraphEmailSender : IEmailSender
    • Inject Graph client using MSAL
    • Replace default sender via DI
     Configuration.ReplaceService<IEmailSender, GraphEmailSender>(DependencyLifeStyle.Transient);
    

    Pros

    • Fully compliant with Exchange Online security
    • No SMTP, no MFA issues
    • Works well for service/system emails

    Cons

    • Custom implementation required (but isolated and clean)

    Azure Communication Services

    A solid Microsoft native alternative if Graph is not desired.

    • Uses API key auth
    • No dependency on Exchange users
    • Email domain must be verified in Azure

    Same pattern: custom IEmailSender implementation.

    Pros

    • Very simple auth model
    • Designed for transactional emails

    Cons

    • Separate email infrastructure from Exchange
    • Slightly higher operational overhead

    External SMTP Providers

    Providers such as:

    • SendGrid
    • Amazon SES
    • Mailgun

    These still support SMTP or API keys and work out of the box with MailKit.

    Pros

    • Minimal or zero code changes
    • Immediate fix

    Cons

    • Not Microsoft native
    • Additional external dependency

    All options integrate cleanly into ASP.NET Zero without breaking upgrades or framework compatibility.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    geoteam created

    Hello oguzhanagir,

    thank you very much for your detailed response. I think that the Graph API is a good choice in this case. However, this also means that the configuration page for SMTP in the host login becomes unnecessary and can be hidden.

    Kind regards, Frank

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @geoteam

    You’re absolutely right. When switching to Microsoft Graph API, the SMTP configuration is no longer used.

    In ASP.NET Zero, the SMTP settings page is mainly a UI over the default MailKit based IEmailSender. If you replace IEmailSender with a Graph based implementation, those settings are simply ignored at runtime.

    From an implementation perspective, you have two clean options:

    • Hide or disable the SMTP settings UI (recommended), since it no longer reflects the active email mechanism.
    • Leave it visible but unused, for backward compatibility or future fallback, without affecting the Graph based sender.

    Most customers who move to Graph choose to hide the SMTP section to avoid confusion for administrators.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)