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.
- 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 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.
- using MailKit.Net.Smtp;
- using MimeKit;
- using System;
-
- namespace EmailApplication
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
-
- string FromAddress = "From Email Address";
- string FromAdressTitle = "Email from ASP.NET Core 1.1";
-
- 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-connected applications like IOT, web apps, and mobile back-end.";
-
-
- string SmtpServer = "smtp.gmail.com";
-
- 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);
-
-
- 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 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".
- 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.
- <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
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.