FluentEmail icon indicating copy to clipboard operation
FluentEmail copied to clipboard

Using multiple SMTP credentials

Open sjd2021 opened this issue 2 years ago • 1 comments
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?

sjd2021 avatar Mar 02 '23 10:03 sjd2021

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.

manio143 avatar May 13 '23 22:05 manio143