Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Using office365 smtp #3962


User avatar
0
evadmin created

Hi, I've configured my site to use smtp.office365.com as my smtp server, but it doesn't seem to work and I can't figure out how to debug it. When I try to send test emails, there's a notification saying they are sent successfully, but none of the emails I've sent have been received.


3 Answer(s)
  • User Avatar
    0
    luckyduck created

    Have you sent this post [https://forum.aspnetboilerplate.com/viewtopic.php?f=5&t=6830&p=15927&hilit=office365+ab#p22321])?

    Based on this, I went direct using this article as a guide - [https://weblogs.asp.net/sreejukg/send-email-using-office-365-account-and-c]). Also refer to [https://stackoverflow.com/questions/30342884/the-server-response-was-5-7-57-smtp-client-was-not-authenticated-to-send-anony]) in case you run into authentication issues (which I did).

    I still intend to use ABP SettingManager to store the config settings.

  • User Avatar
    0
    evadmin created

    The problem I encountered is based on some quirks of using smtp.Office365.com. After reading a bit about sending emails with Office365 and MailKit, I solved my problem by replacing DefaultMailKitSmtpBuilderwith a custom one that's exactly the same with the addition of two lines:

    client.ServerCertificateValidationCallback = (s, c, h, e) => true; and client.AuthenticationMechanisms.Remove("XOAUTH2");

    Here's my complete Office365SmtpBuilder implementation:

    public class Office365SmtpBuilder : IMailKitSmtpBuilder
        {
            private readonly ISmtpEmailSenderConfiguration _smtpEmailSenderConfiguration;
    
            public Office365SmtpBuilder(ISmtpEmailSenderConfiguration smtpEmailSenderConfiguration)
            {
                _smtpEmailSenderConfiguration = smtpEmailSenderConfiguration;
            }
    
            public virtual SmtpClient Build()
            {
                var client = new SmtpClient();
    
                try
                {
                    ConfigureClient(client);
                    return client;
                }
                catch
                {
                    client.Dispose();
                    throw;
                }
            }
    
            protected virtual void ConfigureClient(SmtpClient client)
            {
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;  //added this line
    
                client.Connect(
                    _smtpEmailSenderConfiguration.Host,
                    _smtpEmailSenderConfiguration.Port,
                    _smtpEmailSenderConfiguration.EnableSsl
                );
    
                client.AuthenticationMechanisms.Remove("XOAUTH2");  //added this line
    
                var userName = _smtpEmailSenderConfiguration.UserName;
                if (!userName.IsNullOrEmpty())
                {
                    client.Authenticate(
                        _smtpEmailSenderConfiguration.UserName,
                        _smtpEmailSenderConfiguration.Password
                    );
                }
            }
        }
    

    I replaced the DefaultMailKitSmtpBuilder service by updating my AdminCoreModule in the PreInitialize() method:

    if (DebugHelper.IsDebug)
                {
                    //Disabling email sending in debug mode
                    Configuration.ReplaceService<IMailKitSmtpBuilder, Office365SmtpBuilder>(DependencyLifeStyle.Transient);
                }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks for sharing your solution @evadmin.