FluentEmail
FluentEmail copied to clipboard
Using multiple SMTP credentials
trafficstars
In my application, users need to be able to supply their own SMTP host/credentials for their company. Currently, I don't see a way to support using different SMTP servers to send emails at "runtime" (post-DI). Am I overlooking something?
You can configure the ISender interface using any other service at runtime. Example:
services.AddOptions<SmtpSettings>();
services.AddScoped<ISender>((IServiceProvider provider) =>
{
var settings = provider.GetRequiredService<IOptionsSnapshot<SmtpSettings>>().Value;
var client = new SmtpClient()
{
Host = settings.Host,
Port = settings.Port,
};
return new SmtpSender(client);
});
You can define your own service to supply the right credential in the context you're in.