Send Email Using ASP.NET CORE 1.1 With MailKit In Visual Studio 2017

Introduction
 
We are familiar with Sending Email Using ASP.NET With C#. But today, we are going to teach you how to send email using ASP.NET Core 1.1 with MailKit. We can implement it in ASP.Net Core very easily as compared to the previous versions of ASP.NET.
 
Before reading this article, you must read the articles given below for ASP.NET Core knowledge.
MailKit
 
MailKit is a cross-platform mail client library built on top of MimeKit. That means we get all the mail sending libraries from MailKit, such as - Simple Mail Transfer Protocol (SMTP) etc.
 
Simple Mail Transfer Protocol (SMTP)
 
Simple Mail Transfer Protocol (SMTP) is a TCP/IP protocol used in sending and receiving e-mail. Most e-mail systems that send mail over the Internet use SMTP to send messages from one server to another.The messages can then be retrieved with an e-mail client using either POP or IMAP.
 
The following is a list of SMTP Server and Port Numbers,
 
Sl.No Mail Server SMTP Server( Host ) Port Number
1 Gmail smtp.gmail.com 587
2 Outlook smtp.live.com 587
3 Yahoo Mail smtp.mail.yahoo.com 465
4 Yahoo Mail Plus plus.smtp.mail.yahoo.com 465
5 Hotmail smtp.live.com 465
6 Office365.com smtp.office365.com 587
7 zoho Mail smtp.zoho.com 465

Assemblies Required
 
The following assemblies are required for sending email using ASP.NET Core with MailKit.
  1. using MailKit.Net.Smtp;  
  2. using MimeKit;  
Adding MailKit in Our Project
 
Go to "Tools -> NuGet Package Manager -> Manage Nuget Package for Solutions…" Then, search "MailKit", Choose and Install the latest version "V1.12.0" in your application.
 
 
 
Project Structure
 
New .NET Core tooling is available in Visual Studio 2017 by default. In the Dependencies folder, every package tool has separate folder like MailKit saved into NuGet folder. If you have a client side tool like bower, then its dependencies are saved into it’s folder.
 
 
Code
 
The following code contains the mail sending code of ASP.NET Core.
  1. using MailKit.Net.Smtp;  
  2. using MimeKit;  
  3. using System;  
  4.    
  5. namespace EmailApplication  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             try  
  12.             {  
  13.                 //From Address  
  14.                 string FromAddress = "From Email Address";  
  15.                 string FromAdressTitle = "Email from ASP.NET Core 1.1";  
  16.                 //To Address  
  17.                 string ToAddress = "To Email Address";  
  18.                 string ToAdressTitle = "Microsoft ASP.NET Core";  
  19.                 string Subject = "Hello World - Sending email using ASP.NET Core 1.1";  
  20.                 string BodyContent = "ASP.NET Core was previously called ASP.NET 5. It was renamed in January 2016. It supports cross-platform frameworks ( Windows, Linux, Mac ) for building modern cloud-based internet-connected applications like IOT, web apps, and mobile back-end.";  
  21.    
  22.                 //Smtp Server  
  23.                 string SmtpServer = "smtp.gmail.com";  
  24.                 //Smtp Port Number  
  25.                 int SmtpPortNumber = 587;  
  26.    
  27.                 var mimeMessage = new MimeMessage();  
  28.                 mimeMessage.From.Add(new MailboxAddress(FromAdressTitle, FromAddress));  
  29.                 mimeMessage.To.Add(new MailboxAddress(ToAdressTitle, ToAddress));  
  30.                 mimeMessage.Subject = Subject;  
  31.                 mimeMessage.Body = new TextPart("plain")  
  32.                 {  
  33.                     Text = BodyContent  
  34.    
  35.                 };  
  36.    
  37.                 using (var client = new SmtpClient())  
  38.                 {  
  39.    
  40.                     client.Connect(SmtpServer, SmtpPortNumber, false);  
  41.                     // Note: only needed if the SMTP server requires authentication  
  42.                     // Error 5.5.1 Authentication   
  43.                     client.Authenticate("From Address Email""Password");  
  44.                     client.Send(mimeMessage);  
  45.                     Console.WriteLine("The mail has been sent successfully !!");  
  46.                     Console.ReadLine();  
  47.                     client.Disconnect(true);  
  48.    
  49.                 }  
  50.             }  
  51.             catch (Exception ex)  
  52.             {  
  53.                 throw ex;  
  54.             }  
  55.         }  
  56.     }  
  57. }  
Important Points
  • When you are sending mail with your "gmail" account, enable less secure apps so that you will be able to login from all apps. Otherwise, it will throw authentication error like 5.5.1 authentication.

  • Remove 2-Step Verification.
In the following code, we can mention username for "gmail" account but in other service like "hotmail", we must provide the full email address because other Microsoft accounts, like Outlook, live, etc. have the same SMTP Server Address "smtp.live.com".
  1. client.Authenticate("From Address Email""Password");  
csproj

In previous version, ASP.NET Core 1.0 contained all the versions & dependencies in project.json file but in new version, i.e., ASP.NET Core 1.1, they are saved in csproj.
  1. <Project Sdk="Microsoft.NET.Sdk">  
  2.    
  3.   <PropertyGroup>  
  4.     <OutputType>Exe</OutputType>  
  5.     <TargetFramework>netcoreapp1.1</TargetFramework>  
  6.   </PropertyGroup>  
  7.    
  8.   <ItemGroup>  
  9.     <PackageReference Include="MailKit" Version="1.12.0" />  
  10.   </ItemGroup>  
  11.    
  12. </Project>  
Output
 
 
 
References
Conclusion
 
We learned how to send email using ASP.NET Core 1.1 with MailKit in Visual Studio 2017. I hope, you liked this article. Please share your valuable suggestions and feedback.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now