Skip to main content

Posts

Showing posts from July, 2022

Send an Email via SMTP with MailKit Using .NET 6

How to Send an Email in .NET Core This tutorial show you how to send an email in .NET 6.0 using the MailKit email client library. Install MailKit via NuGet Visual Studio Package Manager Console: Install-Package MailKit How to Send an HTML Email in .NET 6.0 This code sends a simple HTML email using the Gmail SMTP service. There are instructions further below on how to use a few other popular SMTP providers - Gmail, Hotmail, Office 365. // create email message var email = new MimeMessage(); email.From.Add(MailboxAddress.Parse("from_address@example.com")); email.To.Add(MailboxAddress.Parse("to_address@example.com")); email.Subject = "Email Subject"; email.Body = new TextPart(TextFormat.Html) { Text = "<h1>Test HTML Message Body</h1>" }; // send email using var smtp = new SmtpClient(); smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls); smtp.Authenticate("[Username]", "[Password]"); smtp.Se

CRUD operations in web api using net 6.0

.NET 6.0 - CRUD API Example and Tutorial This tutorial gives you step by step implementation of creating CRUD operations using ASP.NET Web API Core. This tutorial builds API for its clients to manage employee. it shows how to configure the SQL server connection to manage CRUD operations in real-time applications. Let's start step by step. 1. Create ASP.NET Web API Project Open your Visual Studio and select New Project from File Menu.     Now enter the name of the application and click next.   Next select framework .net 6 from drop down and enable open API (Swagger) support and click on create to create default project template for API.   2. Now Add Model to Employee Web API In this step, you will add the Employee Model Class which will define database entity. Right click on the project from solution explorer and select Add -> New Folder . Enter folder name as Models . Right click on the Models folder and select Add -> Class and add class file with name

How to do Alignment within string.Format in C#

Align String with Spaces [C#] This example shows how to align strings with spaces. The example formats text to table and writes it to console output. To align string to the right or to the left use static method String.Format. To align string to the left (spaces on the right), use negative number like below String.Format("{0,–10}", text) To align string to the right (spaces on the left), use postive number like below String.Format("{0,10}", text) Following example shows how to format text to the table. Values in the first and second column are aligned to the left and the third column is aligned to the right. Console.WriteLine("-------------------------------"); Console.WriteLine("First Name | Last Name | Age"); Console.WriteLine("-------------------------------"); Console.WriteLine(String.Format("{0,-10} | {1,-10} | {2,5}", "Adam", "Gilchrist", 50)); Console.WriteLine(String.Format("{0,-10