Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
- ASP.NET CORE 1.0: Getting Started
- ASP.NET Core 1.0: Project Layout
- ASP.NET Core 1.0: Middleware And Static files (Part 1)
- Middleware And Staticfiles In ASP.NET Core 1.0 - Part Two
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.
using MailKit.Net.Smtp;
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 a separate folder as MailKit saved into NuGet folder. If you have a client-side tool like Bower, then its dependencies are saved into its folder.
Code
The following code contains the mail sending code of ASP.NET Core.
using MailKit.Net.Smtp;
using MimeKit;
using System;
namespace EmailApplication
{
class Program
{
static void Main(string[] args)
{
try
{
//From Address
string FromAddress = "From Email Address";
string FromAdressTitle = "Email from ASP.NET Core 1.1";
//To Address
string ToAddress = "To Email Address";
string ToAdressTitle = "Microsoft ASP.NET Core";
string Subject = "Hello World - Sending email using ASP.NET Core 1.1";
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-connectedapplications like IOT, web apps, and mobile back-end.";
//Smtp Server
string SmtpServer = "smtp.gmail.com";
//Smtp Port Number
int SmtpPortNumber = 587;
var mimeMessage = new MimeMessage();
mimeMessage.From.Add( new MailboxAddress(FromAdressTitle, FromAddress));
mimeMessage.To.Add( new MailboxAddress(ToAdressTitle, ToAddress));
mimeMessage.Subject = Subject;
mimeMessage.Body = new TextPart("plain")
{
Text = BodyContent
};
using (var client = new SmtpClient())
{
client.Connect(SmtpServer, SmtpPortNumber, false );
// Note: only needed if the SMTP server requires authentication
// Error 5.5.1 Authentication
client.Authenticate( "From Address Email" , "Password" );
client.Send(mimeMessage);
Console.WriteLine( "The mail has been sent successfully !!" );
Console.ReadLine();
client.Disconnect( true );
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
Important Points
- When you are sending a mail with your "Gmail" account, enable less secure apps so that you will be able to log in 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 another 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".
client.Authenticate("From Address Email", "Password");
csproj
In the previous version, ASP.NET Core 1.0 contained all the versions and dependencies in the project.json file but in the new version, i.e., ASP.NET Core 1.1, they are saved in the csproj.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MailKit" Version="1.12.0" />
</ItemGroup>
</Project>
Output
Reference
Downloads
You can download ASP.NET Core 1.0 source code from the MSDN Code, using the links, mentioned below.
- Middleware And Staticfiles In ASP.NET Core 1.0 - Part One
- Middleware And Staticfiles In ASP.NET Core 1.0 - Part Two
- Create An Aurelia Single Page Application In ASP.NET Core 1.0
- Create Rest API Or Web API With ASP.NET Core 1.0
- Adding A Configuration Source File In ASP.NET Core 1.0
- Code First Migration - ASP.NET Core MVC With EntityFrameWork Core
- Building ASP.NET Core MVC Application Using EF Core and ASP.NET Core 1.0
See Also
It's recommended to read more articles related to ASP.NET Core.
- ASP.NET CORE 1.0: Getting Started
- ASP.NET Core 1.0: Project Layout
- ASP.NET Core 1.0: Middleware And Static files (Part 1)
- Middleware And Staticfiles In ASP.NET Core 1.0 - Part Two
- ASP.NET Core 1.0 Configuration: Aurelia Single Page Applications
- ASP.NET Core 1.0: Create An Aurelia Single Page Application
- Create Rest API Or Web API With ASP.NET Core 1.0
- ASP.NET Core 1.0: Adding A Configuration Source File
- Code First Migration - ASP.NET Core MVC With EntityFrameWork Core
- Building ASP.NET Core MVC Application Using EF Core and ASP.NET Core 1.0