Advertisements

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.

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.

MailKit Installation

MailKit Installation

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.

Project Structure ASP.NET Core 1.1

Project Structure ASP.NET Core 1.1

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-connected applications 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 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.

Advertisements

Visual Studio 2017 : New Features & Installation


Introduction

Feeling great & Looking Smart. I know every one is excited for the new release of “Visual Studio 2017”. Yes , Microsoft Visual Studio celebrated it’s 20th years of glory with all tech lovers in the world love it. Now, it has come up with lots of excitement. Thus, I am happy to share the important features of Visual Studio 2017 with all the tech lovers.

Visual Studio 2017 System Requirements

The following are the minimum requirements of Visual Studio 2017 installation.

Download Visual Studio 2017

Go to this link Visual Studio 2017 , choose & download your favorite edition.

Installing Visual Studio 2017

Visual Studio 2017 Installation Window shows 3 sections.

  1. Workloads
  2. Individual components
  3. Language packs

Workloads

We can choose the appropriate development section from Visual Studio Workloads. I selected “ASP.NET and Web development & .NET Core cross-platform development”. It also contains around 5.50 GB. Thus, If you select all Workloads, then it will reach 50 GB+.

Individual components

We can choose the required component from this section.

Language packs

We can use different languages in Visual Studio. Thus, we can choose the appropriate language in this section.

Installation Process

We are going to install Visual Studio Community 2017 edition in our machine. Ones the following process is complete, we can launch & access the new features in Visual Studio 2017.

Features in Visual Studio 2017

The following are the new features in Visual Studio 2017.

Live Unit Testing

This feature is only available in Visual Studio 2017 Enterprise edition. Without running the solution, we can easily test the Application because few hidden background process happens in our Application through “Live Unit Testing”. We can enable Live Unit Testing for which you need to go to the “Test” command of the top-level menu bar in VS, choose “Live Unit Testing”, then “Start”.

Image Credit : Channel 9 Visual Studio 2017 Lanuch

Image Credit : Channel 9 Visual Studio 2017 Lanuch

We noticed that 3 symbols appeared in our code due to running on background Live Testing.

  • Cross Sign – line of executable code. The test is covered and it indicates that a failure test.
  • Tick Mark – line of executable code. The test is covered and it indicates that a success test.
  • Minus – line of executable code. Not yet covered a single Test.

Dotted Line

The is a very cool feature in Visual Studio 2017 because this option helps to identify the exact open and close curly braces “{}” through dotted line in our code. This dotted line is really helpful for when you are working on a bulk amount of code in a single page.

Filttered IntelliSense Search

The new Filtering IntelliSense Search option is available in Visual Studio 2017. This option is one of the powerful way to consume the search time in our code. The following IntelliSense bottom tray contains icons as Properties, Interface, Modules, Namespaces, Enums, Classes, Constants etc. For example, if i want to search an Interface, then I can choose an Interface Icon from the bottom of the tray, else it will show all the possible contents in our solution.

Improvements Of Navigation Controls

  1. Go to All ( Cntrl + T )
  2. Go to Line ( Cntrl + G )
  3. Go to File ( Cntrl + 1 + F )
  4. Go to Type ( Cntrl + 1 + T )
  5. Go to Member ( Cntrl + 1 + M )
  6. Go to Symbol ( Cntrl + 1 + S )

Go to All ( Cntrl + T )

Press “Cntrl + T”, then it will display one tray above the search bar. Thus, we can search the content with the help of the options given below like Line/Files/Types/Members/Symbols. For example, I want to search type in our entire solution or an Application. Now, choose “Type” Icon in the tray and type in the search bar, so it will filter and show the exact results as much as faster than the earlier versions of VS. The shortcut is “Cntrl + 1 + T”.

Find All Reference ( Shift + F12 )

We can find all the references in our entire solution. The highlighted one is all the reference results by a combination of project, definition and path.

Code Suggestions

This is a very exciting feature in VS2017. It gives us the good code practice in our Application. The code given below notices that we have written an “If” condition without curl braces. Thus, it gives us the suggestion tp add the curl braces to understand the code.

Code Style Changing

We can change the code style option in our VS2017. Go to “Tools -> Options -> Text Editor -> Basic -> Code Style -> Naming”.

Reference

Conclusion

We learnt installation & new features of Visual Studio 2017. I hope, you liked this article. Please share your valuable suggestions and feedback.

%d bloggers like this: