Skip to content

Commit

Permalink
Section 12 - Email Sender using MimeKit and MailKit
Browse files Browse the repository at this point in the history
  • Loading branch information
bhrugen committed Sep 27, 2021
1 parent 48b89b1 commit b51be3d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions BulkyBook.Utility/BulkyBook.Utility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MailKit" Version="2.15.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.0-rc.1.21452.15" />
<PackageReference Include="MimeKit" Version="2.15.1" />
</ItemGroup>

</Project>
20 changes: 19 additions & 1 deletion BulkyBook.Utility/EmailSender.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Identity.UI.Services;
using MailKit.Net.Smtp;
using Microsoft.AspNetCore.Identity.UI.Services;
using MimeKit;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -11,7 +13,23 @@ public class EmailSender : IEmailSender
{
public Task SendEmailAsync(string email, string subject, string htmlMessage)
{
var emailToSend = new MimeMessage();
emailToSend.From.Add(MailboxAddress.Parse("[email protected]"));
emailToSend.To.Add(MailboxAddress.Parse(email));
emailToSend.Subject = subject;
emailToSend.Body = new TextPart(MimeKit.Text.TextFormat.Html){ Text = htmlMessage};

//send email
using (var emailClient = new SmtpClient())
{
emailClient.Connect("smtp.gmail.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
emailClient.Authenticate("[email protected]", "DotNet213$");
emailClient.Send(emailToSend);
emailClient.Disconnect(true);
}

return Task.CompletedTask;

}
}
}

0 comments on commit b51be3d

Please sign in to comment.