Skip to content

Commit 7a386b0

Browse files
committed
[ASP.NET Core] Improved SendGridEmailSender to receive the sender as Send() parameter instead of in constructor
1 parent 69a6633 commit 7a386b0

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

ASP.NET Core/Services/AspNetCoreTemplate.Services.Messaging/IEmailSender.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
public interface IEmailSender
77
{
8-
Task SendEmailAsync(string to, string subject, string htmlContent, IEnumerable<EmailAttachment> attachments = null);
8+
Task SendEmailAsync(
9+
string from,
10+
string fromName,
11+
string to,
12+
string subject,
13+
string htmlContent,
14+
IEnumerable<EmailAttachment> attachments = null);
915
}
1016
}

ASP.NET Core/Services/AspNetCoreTemplate.Services.Messaging/NullMessageSender.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55

66
public class NullMessageSender : IEmailSender
77
{
8-
public Task SendEmailAsync(string to, string subject, string htmlContent, IEnumerable<EmailAttachment> attachments = null)
8+
public Task SendEmailAsync(
9+
string from,
10+
string fromName,
11+
string to,
12+
string subject,
13+
string htmlContent,
14+
IEnumerable<EmailAttachment> attachments = null)
915
{
1016
return Task.CompletedTask;
1117
}

ASP.NET Core/Services/AspNetCoreTemplate.Services.Messaging/SendGridEmailSender.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,21 @@ public class SendGridEmailSender : IEmailSender
1212
{
1313
private readonly SendGridClient client;
1414

15-
private readonly EmailAddress from;
16-
17-
public SendGridEmailSender(string apiKey, string fromAddress, string fromName)
15+
public SendGridEmailSender(string apiKey)
1816
{
1917
this.client = new SendGridClient(apiKey);
20-
this.from = new EmailAddress(fromAddress, fromName);
2118
}
2219

23-
public async Task SendEmailAsync(string to, string subject, string htmlContent, IEnumerable<EmailAttachment> attachments = null)
20+
public async Task SendEmailAsync(string from, string fromName, string to, string subject, string htmlContent, IEnumerable<EmailAttachment> attachments = null)
2421
{
2522
if (string.IsNullOrWhiteSpace(subject) && string.IsNullOrWhiteSpace(htmlContent))
2623
{
2724
throw new ArgumentException("Subject and message should be provided.");
2825
}
2926

27+
var fromAddress = new EmailAddress(from, fromName);
3028
var toAddress = new EmailAddress(to);
31-
var message = MailHelper.CreateSingleEmail(this.from, toAddress, subject, null, htmlContent);
29+
var message = MailHelper.CreateSingleEmail(fromAddress, toAddress, subject, null, htmlContent);
3230
if (attachments?.Any() == true)
3331
{
3432
foreach (var attachment in attachments)

0 commit comments

Comments
 (0)