Base solution for your next web application
Open Closed

How to implement ISmtpEmailSender in application service? #17


User avatar
0
mohamed emaish created

i'm sorry i couldn't figure how to use ISmtpEmailSender in application service, i tried to inject it in my application service class but it's not registered. i had a look on the Test class, but i couldn't also inject ISmtpEmailSenderConfiguration but it's not registered. how to implement ISmtpEmailSender in application service layer and other layers Thank you


13 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    I published v0.5.8.1 to fix it. I just was not register email sender. Now, just inject IEmailSender or ISmtpEmailSender and use it. It will work if settings are correct.

  • User Avatar
    0
    mohamed emaish created

    Thank you

  • User Avatar
    0
    gvb created

    Is it possible to know how to configure the setting for the SMTP service?

    I know i need to inject the service in the ApplicationService, but how do i configure which server to use and credential or port?

    thx in advance!

  • User Avatar
    0
    hikalkan created
    Support Team

    Using setting system (ISettingManager). I'll document it soon.

  • User Avatar
    0
    gvb created

    I didn't find anything usefull in ISettingManager by creating a class then heritate ISettingManager....

    The only thing i found usefull is ISmtpEmailSenderConfiguration but i dont know how to use it.... and how to link ISettingManager and ISmtpEmailSenderConfiguration!?

    i just found in the doc this part, why configure it there if there is a ISmtpEmailSenderConfiguration?

    public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
        {
            return new[]
                    {
                        new SettingDefinition(
                            "SmtpServerAddress",
                            "127.0.0.1"
                            ),
                ...
        }
    
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    As I said, I haven't documented yet. So, it's a little long topic. But, briefly, ISmtpEmailSenderConfiguration is an abstraction. You can implement it to get configuration from any source. Default implementation gets configuration from ISettingManager. If you want to override it, implement ISmtpEmailSenderConfiguration yourself and register it to DI on PreInitialize of your module. ISettingManager needs to ISettingStore be implemented. It's implemented by module-zero.

    So, as I said it's a little long topic. For now, you can use your own way to send emails if you can not solve the structure yourself. But I'll prepare docs in a few weeks.

  • User Avatar
    0
    omital created

    Hi, Did any documentation about ISmtpEmailSender?

  • User Avatar
    0
    chrisk created

    Hi,

    I'm using IEmailSender with success which in turn by default uses SmtpEmailSender. In my case added email functionality to existing notification infrastructure.

    Check this [https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp/Net/Mail/EmailSettingProvider.cs]) and [https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp/Net/Mail/Smtp/SmtpEmailSender.cs])

    All I had to do to get emails to work was to override some of default settings.

    _settingManager.ChangeSettingForApplication(EmailSettingNames.Smtp.Host, "");
                _settingManager.ChangeSettingForApplication(EmailSettingNames.Smtp.UserName, "");
                _settingManager.ChangeSettingForApplication(EmailSettingNames.Smtp.Password, "");
                _settingManager.ChangeSettingForApplication(EmailSettingNames.DefaultFromAddress, "");
                _settingManager.ChangeSettingForApplication(EmailSettingNames.DefaultFromDisplayName, "");
    

    To create email content either in IApplicationService or anywhere else I created :

    public interface IEmailParser
        {
            MailMessage Parse(TenantNotification notification);
        }
    

    And lastly I implemented IEmailParser in my Web project using Postal

    https://github.com/andrewdavey/postal
    

    Hope that helps

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Omital,

    Currently we don't have a documentation for that but thanks to @Chrisk, he explained it very well :). If you face any problems using it, please write back.

  • User Avatar
    0
    gouse created

    Can u please let me know how to replace default host, port etc details of smtp?

    I have downloaded recently...

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Just insert setting values to AbpSettings table with the matching setting names.

  • User Avatar
    0
    omital created

    Hi @ismcagdas I implement it manually by my own way like this:

    public class LIMSEmailSender : ILIMSEmailSender
        {
    
            private readonly SenderEmailConfig _senderEmailConfig;
    
            public LIMSEmailSender(SenderEmailConfig senderEmailConfig)
            {
                _senderEmailConfig = senderEmailConfig;
            }
    
            public void SendEmail(string to, string subject, string body)
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient(_senderEmailConfig.SmtpClient);
                mail.From = new MailAddress(_senderEmailConfig.EmailAddress);
                mail.To.Add(to);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;
    
                SmtpServer.Port = _senderEmailConfig.SmtpPort;
                SmtpServer.Credentials = new System.Net.NetworkCredential(_senderEmailConfig.EmailAddress, _senderEmailConfig.Password);
                SmtpServer.EnableSsl = _senderEmailConfig.EnableSSL;
                SmtpServer.Send(mail);
            }
        }
    

    and SenderEmailConfig implementation:

    public class SenderEmailConfig : ISingletonDependency
        {
            public string EmailAddress { get; set; }
            public string Password { get; set; }
            public string SmtpClient { get; set; }
            public bool EnableSSL { get; set; }
            public int SmtpPort { get; set; }
        }
    

    and initial SenderEmailConfig in web module's PreInitialize method like this

    var limsEmail = IocManager.Resolve<SenderEmailConfig>();
                limsEmail.EmailAddress= System.Configuration.ConfigurationManager.AppSettings["email"];
                limsEmail.EnableSSL = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["enableSSL"]);
                limsEmail.Password = System.Configuration.ConfigurationManager.AppSettings["emailPassword"];
                limsEmail.SmtpClient = System.Configuration.ConfigurationManager.AppSettings["smtpClient"];
                limsEmail.SmtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["smtpPort"]);
    

    and finally I love This Framework

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thank you for sharing your code :)