forked from bhrugen/Bulky
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Section 12 - Email Sender using MimeKit and MailKit
- Loading branch information
Showing
2 changed files
with
21 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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; | ||
|
||
} | ||
} | ||
} |