Skip to content

Commit

Permalink
Updated Async usage to remove use of Task.Run()
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchelsellers committed May 29, 2024
1 parent 12c5bd8 commit eec645d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task SendToAdministratorAsync_ShouldSend_DefaultingFromAndToAddress

//Verify
_mimeMessageFactoryMock.Verify();
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
_mimeKitServiceMock.Verify(k => k.SendEmailAsync(mimeMessage));
}

[Fact]
Expand All @@ -96,7 +96,7 @@ public async Task SendToAdministrator_ShouldSend_DefaultingFromAndToAddress_With

//Verify
_mimeMessageFactoryMock.Verify();
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
_mimeKitServiceMock.Verify(k => k.SendEmailAsync(mimeMessage));
}

[Fact]
Expand All @@ -116,7 +116,7 @@ public async Task SendMessage_WithoutCCRecipients_ShouldSend_DefaultingFromAddre

//Verify
_mimeMessageFactoryMock.Verify();
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
_mimeKitServiceMock.Verify(k => k.SendEmailAsync(mimeMessage));
}

[Fact]
Expand All @@ -141,7 +141,7 @@ public async Task SendMessageWithReplyTo_WithoutCCRecipients_ShouldSend_Defaulti
var replyToAsAdded = mimeMessage.ReplyTo.First();
Assert.Equal("\"Bob\" <me@me.com>", replyToAsAdded.ToString());
_mimeMessageFactoryMock.Verify();
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
_mimeKitServiceMock.Verify(k => k.SendEmailAsync(mimeMessage));
}

[Fact]
Expand All @@ -162,7 +162,7 @@ public async Task SendMessage_WithCCRecipients_ShouldSend_DefaultingFromAddress(

//Verify
_mimeMessageFactoryMock.Verify();
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
_mimeKitServiceMock.Verify(k => k.SendEmailAsync(mimeMessage));
}

[Fact]
Expand All @@ -188,7 +188,7 @@ public async Task SendMessageWithReplyTo_WithCCRecipients_ShouldSend_DefaultingF
var replyToAsAdded = mimeMessage.ReplyTo.First();
Assert.Equal("\"Bob\" <me@me.com>", replyToAsAdded.ToString());
_mimeMessageFactoryMock.Verify();
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
_mimeKitServiceMock.Verify(k => k.SendEmailAsync(mimeMessage));
}

[Fact]
Expand All @@ -211,7 +211,7 @@ public async Task SendMessageWithAttachment_ShouldSend_DefaultingFromAddress()

//Assets
_mimeMessageFactoryMock.Verify();
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
_mimeKitServiceMock.Verify(k => k.SendEmailAsync(mimeMessage));
}

[Fact]
Expand All @@ -233,7 +233,7 @@ public async Task SendMessage_ShouldPassOptionalTemplateName_ToMessageMethods()

//Assets
_mimeMessageFactoryMock.Verify();
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
_mimeKitServiceMock.Verify(k => k.SendEmailAsync(mimeMessage));
}

[Fact]
Expand All @@ -260,7 +260,7 @@ public async Task SendMessageWithReplyTo_ShouldPassOptionalTemplateName_ToMessag
var replyToAsAdded = mimeMessage.ReplyTo.First();
Assert.Equal("\"Bob\" <me@me.com>", replyToAsAdded.ToString());
_mimeMessageFactoryMock.Verify();
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
_mimeKitServiceMock.Verify(k => k.SendEmailAsync(mimeMessage));
}

[Fact]
Expand All @@ -284,7 +284,7 @@ public async Task SendMessageWithAttachment_ShouldPassOptionalTemplateName_ToMes

//Assets
_mimeMessageFactoryMock.Verify();
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
_mimeKitServiceMock.Verify(k => k.SendEmailAsync(mimeMessage));
}
}
}
11 changes: 6 additions & 5 deletions src/NetCore.Utilities.Email.Smtp/MimeKitService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using MailKit.Net.Smtp;
using Microsoft.Extensions.Options;
using MimeKit;
Expand All @@ -14,7 +15,7 @@ public interface IMimeKitService
/// Sends an email message
/// </summary>
/// <param name="toSend">The message to send</param>
void SendEmail(MimeMessage toSend);
Task SendEmailAsync(MimeMessage toSend);
}

/// <summary>
Expand All @@ -38,13 +39,13 @@ public MimeKitService(IOptions<SmtpServiceOptions> configuration)
}

/// <inheritdoc />
public void SendEmail(MimeMessage toSend)
public async Task SendEmailAsync(MimeMessage toSend)
{
using var client = new SmtpClient();
client.Connect(_configuration.Value.Server, _configuration.Value.Port, _configuration.Value.UseSsl);
await client.ConnectAsync(_configuration.Value.Server, _configuration.Value.Port, _configuration.Value.UseSsl);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(_configuration.Value.SenderUsername, _configuration.Value.SenderPassword);
client.Send(toSend);
await client.AuthenticateAsync(_configuration.Value.SenderUsername, _configuration.Value.SenderPassword);
await client.SendAsync(toSend);
}
}
}
6 changes: 3 additions & 3 deletions src/NetCore.Utilities.Email.Smtp/SmtpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public async Task<bool> SendMessageAsync(string toAddress, IEnumerable<string> c
subject, bodyHtml, templateName);

//Send
await Task.Run(() => _mimeKitService.SendEmail(toSend));
await _mimeKitService.SendEmailAsync(toSend);

return true; //Success
}
Expand All @@ -113,7 +113,7 @@ public async Task<bool> SendMessageWithAttachmentAsync(string toAddress, IEnumer
ccAddressList, subject, fileContent, fileName, bodyHtml, templateName);

//Send
await Task.Run(() => _mimeKitService.SendEmail(toSend));
await _mimeKitService.SendEmailAsync(toSend);

return true;
}
Expand Down Expand Up @@ -174,7 +174,7 @@ public async Task<bool> SendWithReplyToAsync(string replyToAddress, string reply
}

//Send
await Task.Run(() => _mimeKitService.SendEmail(toSend));
await _mimeKitService.SendEmailAsync(toSend);
return true;
}
}
Expand Down

0 comments on commit eec645d

Please sign in to comment.