在 FluentEmail 中的后续电子邮件中不断添加收件人

Ash*_*h K 1 c# email .net-5

我正在使用FluentEmail发送电子邮件,一切正常,只是存在一个问题,即为每封后续电子邮件不断添加“收件人”收件人。

例如:如果将电子邮件发送至someone@abc.com

  1. 第一次SendEmailAsync调用该方法时,它会将电子邮件发送至:someone@abc.com

  2. 第二次SendEmailAsync调用该方法时,它会将电子邮件发送至:someone@abc.com;someone@abc.com

  3. 第三次SendEmailAsync调用该方法时,它将发送电子邮件至: someone@abc.com;someone@abc.com;someone@abc.com

等等。一段时间后它会变得很长。

我的代码如下所示:

ConfigureServices方法:

// 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() { });
Run Code Online (Sandbox Code Playgroud)

现在电子邮件服务如下所示:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Ash*_*h K 5

使用IFluentEmailFactory而不是IFluentEmail为我解决了问题。

现在电子邮件服务如下所示:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)