Learn How To Send Mail-In ASP.NET CORE MVC using MailKit

In this, article you will learn how to send mail in ASP.NET CORE MVC using MailKit and understand step by step how to install it in our project.

The MailKit is open source NuGet mail Library. it supporting the .NET Framework and.Net Core MVC projects, Now we will understand MailKit classes and implementing our projects.

Create New Project

Let’s understand how to create the new project in ASP.NET CORE MVC in visual studio. so, we have our existing project in ASP.NET CORE MVC.

Install MailKit Library

we need to install the Mailkit library using the Nuget Package Manager console. so we use the following command install MailKit package in our project.

Install-Package MailKit

Then, Add email code in an Action method Let’s go ahead, and create an Action method in the controller.

Public ActionResult SendMail()
{
 return ""; 
}

After that, put the following code into the Action method.

Public ActionResult SendMail()
{ 
    var message = new MimeMessage();
	message.To.AddRange(emailMessage.ToAddresses.Select(x => new MailboxAddress("to email name", " to email address")));
	message.From.AddRange(emailMessage.FromAddresses.Select(x => new MailboxAddress("from email name", "from email address")));
 
	message.Subject = "here write email subject";
	
	message.Body = new TextPart(TextFormat.Html)
	{
		Text = emailMessage.Content
	};
 
	using (var emailClient = new SmtpClient())
	{
		emailClient.Connect("smtp.gmail.com", 465, true);
 
		emailClient.Authenticate("User@mydomain.com", "pass123");

		emailClient.Send(message);
 
		emailClient.Disconnect(true);
	}
} 

If we want to send Text or HTML format in your message body, then we use the “TextFormat“.

Mailkit library has an SMTP class named “SmtpClient” for passing mail authentication and SMTP server address with port. Inside the SmtpClient class have SSL property for connecting to the SMTP server.

Receiving Email Using POP With MailKit

In our application, We have successfully added an SMTP class for sending an email, now we’ll add email receive code using POP using the following ActionResult method to implement the code.

Now, Let’s create ReceiveMail Action Method.

public List<EmailMessage> ReceiveEmail(int maxCount = 15)
{
	using (var emailClient = new Pop3Client())
	{
		emailClient.Connect("Pop Server","Pop Port", true);
 
emailClient.AuthenticationMechanisms.Remove("XOAUTH2");		emailClient.Authenticate("Pop Username","Pop Password");
 
List<EmailMessage> emails = new List<EmailMessage>();
for(int i=0; i < emailClient.Count && i < maxCount; i++)
{
	var message = emailClient.GetMessage(i);
	var emailMessage = new EmailMessage
	{
	Content = !string.IsNullOrEmpty(message.HtmlBody) ? 
        message.HtmlBody : message.TextBody,
	Subject = message.Subject
			};
			emailMessage.ToAddresses.AddRange(message.To.Select(x => (MailboxAddress)x).Select(x => new EmailAddress { Address = x.Address, Name = x.Name }));
			emailMessage.FromAddresses.AddRange(message.From.Select(x => (MailboxAddress)x).Select(x => new EmailAddress { Address = x.Address, Name = x.Name }));
		}
		return emails;
	}
}

So, I hope you understand the MailKit into your ASP.NET CORE application.

Leave a Reply

Your email address will not be published. Required fields are marked *