Sending emails from a web application is a common requirement. It can be used for:
Contact forms
Password reset
Notifications
Email verification
In this tutorial, we will build a complete email sending feature using:
ASP.NET Core Web API for backend
Angular Form for frontend
SMTP with MailKit
Validation and HTML content support
This tutorial is beginner-friendly, practical, and uses clean code practices.
What You Will Build
Angular form to collect user inputs (name, email, message)
ASP.NET Core backend API to receive form data
Email sent using SMTP (supports Gmail, Outlook, or any SMTP server)
HTML email template for professional look
Feedback to user on success or failure
Part 1: Backend Implementation(ASP.NET Core)
Step 1: Create ASP.NET Core API Project
dotnet new webapi -n EmailApi
cd EmailApi
Step 2: Install MailKit
dotnet add package MailKit
MailKit is a popular library for sending emails using SMTP.
Step 3: Configure SMTP Settings
Add your SMTP configuration in appsettings.json:
"Email": {
"From": "[email protected]",
"SmtpServer": "smtp.gmail.com",
"Port": 587,
"Username": "[email protected]",
"Password": "your-email-password"
}
Note: For Gmail, you may need an App Password if 2FA is enabled.
Step 4: Create Email DTO
Models/EmailDto.cs
public class EmailDto
{
public string Name { get; set; }
public string FromEmail { get; set; }
public string Subject { get; set; }
public string Message { get; set; }
}
Step 5: Create Email Service
Services/EmailService.cs
using MailKit.Net.Smtp;
using MimeKit;
public class EmailService
{
private readonly IConfiguration _config;
public EmailService(IConfiguration config)
{
_config = config;
}
public async Task SendEmailAsync(EmailDto dto)
{
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse(_config["Email:From"]));
email.To.Add(MailboxAddress.Parse(_config["Email:From"])); // Send to your own inbox
email.Subject = dto.Subject;
// HTML Body
email.Body = new BodyBuilder
{
HtmlBody = $@"
<h2>Contact Form Submission</h2>
<p><strong>Name:</strong> {dto.Name}</p>
<p><strong>Email:</strong> {dto.FromEmail}</p>
<p><strong>Message:</strong><br>{dto.Message}</p>"
}.ToMessageBody();
using var smtp = new SmtpClient();
await smtp.ConnectAsync(_config["Email:SmtpServer"], int.Parse(_config["Email:Port"]), false);
await smtp.AuthenticateAsync(_config["Email:Username"], _config["Email:Password"]);
await smtp.SendAsync(email);
await smtp.DisconnectAsync(true);
}
}
Step 6: Create Controller
Controllers/EmailController.cs

Join the conversation! Your thoughts help the community grow.