Base solution for your next web application
Open Closed

EmailSender configuration Settings For Tenants #4698


User avatar
0
feliw created

Hi,

I have a problem with IEmailSender using smtp configuration from Host but actually it should use Tenant's smtp configuration. I'm not sure if I missing some configuration or not.

So the idea is I have a background Job which I used to send all email. On the background job's Execute code I put

CurrentUnitOfWork.SetTenantId(args.TenantId);
...
_emailSender.Send(emailReceiver, emailSubject, email.EmailBody);

Although I already SetTenantId, the IEmailSender sending still use the Host SMTP configuration.

So I try to read the SMTP settings from that Tenant by changing code to this

CurrentUnitOfWork.SetTenantId(args.TenantId);
var settings = SettingManager.GetAllSettingValuesForTenantAsync(args.TenantId.Value).Result;
...
_emailSender.Send(emailReceiver, emailSubject, email.EmailBody);

The smtp settings that I get is correct for that Tenant.

Can you advice me how to make IEmailSender to use Tenant smtp configuration when I already set the TenantId ?


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

    Can you wrap your email sending code inside a "_session.Use" block. Please refer to this document, <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Abp-Session#user-identifier">https://aspnetboilerplate.com/Pages/Doc ... identifier</a>.

  • User Avatar
    0
    feliw created

    Hi,

    I cannot find Use method on IAbpSession. Which version is this feature available from ? Any workaround for a lower version of Abp ? Since I don't think we will do update for the moment.

  • User Avatar
    0
    alper created
    Support Team

    Hi, email sender is little piece of code. Just try to use the below code to send email. I grabbed this code from SmtpEmailSenderclass in AspNet Boilerplate.

    public async Task SendMail(MailMessage mail,
                string host,
                int port,
                string userName,
                string password,
                string domain,
                bool enableSsl,
                bool useDefaultCredentials)
            {
                using (var smtpClient = new SmtpClient(host, port))
                {
                    try
                    {
                        if (enableSsl)
                        {
                            smtpClient.EnableSsl = true;
                        }
    
                        if (useDefaultCredentials)
                        {
                            smtpClient.UseDefaultCredentials = true;
                        }
                        else
                        {
                            smtpClient.UseDefaultCredentials = false;
    
                            if (!userName.IsNullOrEmpty())
                            {
                                smtpClient.Credentials = !domain.IsNullOrEmpty()
                                    ? new NetworkCredential(userName, password, domain)
                                    : new NetworkCredential(userName, password);
                            }
                        }
    
                        await smtpClient.SendMailAsync(mail);
                    }
                    catch
                    {
                        smtpClient.Dispose();
                    }
                }
            }