Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using multiple SMTP credentials #366

Open
sjd2021 opened this issue Mar 2, 2023 · 1 comment
Open

Using multiple SMTP credentials #366

sjd2021 opened this issue Mar 2, 2023 · 1 comment

Comments

@sjd2021
Copy link

sjd2021 commented Mar 2, 2023

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?

@manio143
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants