收件人在 FluentEmail 的后续电子邮件中不断添加
To recipients keep getting added on subsequent emails in FluentEmail
我正在使用 FluentEmail 发送电子邮件,一切正常,除了有一个问题,'To' 收件人不断被添加为每个后续电子邮件。
例如:如果它正在向 someone@abc.com
发送电子邮件:
第一次调用 SendEmailAsync
方法时,它发送电子邮件至:someone@abc.com
第二次调用 SendEmailAsync
方法时,它发送电子邮件至:someone@abc.com;someone@abc.com
第三次调用 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() { });
现在电子邮件服务如下所示:
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;
}
}
使用 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;
}
}
我正在使用 FluentEmail 发送电子邮件,一切正常,除了有一个问题,'To' 收件人不断被添加为每个后续电子邮件。
例如:如果它正在向 someone@abc.com
发送电子邮件:
第一次调用
SendEmailAsync
方法时,它发送电子邮件至:someone@abc.com
第二次调用
SendEmailAsync
方法时,它发送电子邮件至:someone@abc.com;someone@abc.com
第三次调用
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() { });
现在电子邮件服务如下所示:
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;
}
}
使用 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;
}
}