Open Closed

How to replace the existing email sender with my own? #260


User avatar
0
alper created
Support Team Director

How can I switch to my custom email sender service, other than the default one.


1 Answer(s)
  • User Avatar
    0
    alper created
    Support Team Director

    You can create a new class inherits from IEmailSender

    public class MyCustomEmailSender : IEmailSender
        {
            public Task SendAsync(string to, string subject, string body, bool isBodyHtml = true)
            {
                throw new System.NotImplementedException();
            }
    
            public Task SendAsync(string @from, string to, string subject, string body, bool isBodyHtml = true)
            {
                throw new System.NotImplementedException();
            }
    
            public Task SendAsync(MailMessage mail, bool normalize = true)
            {
                throw new System.NotImplementedException();
            }
    
            public Task QueueAsync(string to, string subject, string body, bool isBodyHtml = true)
            {
                throw new System.NotImplementedException();
            }
        }
        
    

    alternatively, you can inherit from EmailSenderBase, so you can just override the SendEmailAsync()

        public class MyCustomEmailSender : EmailSenderBase
        {
            public MyCustomEmailSender(IEmailSenderConfiguration configuration, IBackgroundJobManager backgroundJobManager) : base(configuration, backgroundJobManager)
            {
            }
    
            protected override Task SendEmailAsync(MailMessage mail)
            {
               //send your email
            }
        }
    

    And replace the existing implemantation with yours in your Application project's ApplicationModule. Remove the below code section

    #if DEBUG
                context.Services.Replace(ServiceDescriptor.Singleton<IEmailSender, NullEmailSender>());
    #endif
    

    and add

    context.Services.Replace(ServiceDescriptor.Singleton<IEmailSender, MyCustomEmailSender>());
    

Made with ❤️ on ABP v9.1.0-preview. Updated on November 01, 2024, 05:35