lukencode / FluentEmail

All in one email sender for .NET. Supports popular senders (SendGrid, MailGun, etc) and Razor templates.

Home Page:https://lukelowrey.com/dotnet-email-guide-2021/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

To recipients keep getting added on subsequent emails

akhanalcs opened this issue · comments

I'm using FluentEmail to send emails, everything is working well except there's this issue where the 'To' recipients keep getting added for every subsequent email.

For eg: If it's sending emails to someone@abc.com:

  1. The first time the SendEmailAsync method is called, it sends email to: someone@abc.com

  2. The second time the SendEmailAsync method is called, it sends email to: someone@abc.com;someone@abc.com

  3. The third time the SendEmailAsync method is called, it sends email to:
    someone@abc.com;someone@abc.com;someone@abc.com

and so on. It gets really long after some time.

My code looks like this:

ConfigureServices method:

// Set email service using FluentEmail
services.AddFluentEmail("myapp@goodboi.com")
        .AddRazorRenderer(@$"{Directory.GetCurrentDirectory()}/Views/")
        .AddSmtpSender("smtp.somecompanyname.com", 25)
        .AddSmtpSender(new System.Net.Mail.SmtpClient() { });

Now the email service looks like this:

public class FluentEmailService : IFluentEmailService
{
    private readonly IFluentEmail _fluentEmail;
    private readonly ILogger<FluentEmailService> _logger;
    public FluentEmailService(ILogger<FluentEmailService> logger, IFluentEmail fluentEmail)
    {
        _logger = logger;
        _fluentEmail = fluentEmail;
    }

    public async Task<SendResponse> SendEmailAsync<TModel>(string subject, string razorTemplatePath, TModel model, string semicolonSeparatedEmailRecipients)
    {
		var sendResponse = await _fluentEmail
						.To(semicolonSeparatedEmailRecipients)
						.Subject(subject)
						.UsingTemplateFromFile(razorTemplatePath, model)
						.SendAsync();
		return sendResponse;
    }
}

I'm not sure, but I think this could be resolved by using IFluentEmailFactory instead of IFluentEmail.

I'm not sure, but I think this could be resolved by using IFluentEmailFactory instead of IFluentEmail.

Thank you for the answer @McPhale. That exactly solved my issue.

Now the email service looks like this:

public class FluentEmailService : IFluentEmailService
{
    private readonly IFluentEmailFactory _fluentEmailFactory;
    private readonly ILogger<FluentEmailService> _logger;
    public FluentEmailService(ILogger<FluentEmailService> logger, IFluentEmailFactory fluentEmailFactory)
    {
        _logger = logger;
        _fluentEmailFactory = fluentEmailFactory;
    }

    public async Task<SendResponse> SendEmailAsync<TModel>(string subject, string razorTemplatePath, TModel model, string semicolonSeparatedEmailRecipients)
    {
		var sendResponse = await _fluentEmailFactory
                                                .Create()
						.To(semicolonSeparatedEmailRecipients)
						.Subject(subject)
						.UsingTemplateFromFile(razorTemplatePath, model)
						.SendAsync();
		return sendResponse;
    }
}