How to send emails in Dotnet

content ai google send emails
A
Abhijeet Chattarjee

Email Design Specialist & Template Developer

 
June 30, 2025 2 min read

Introduction

.NET and .NET Core come with built-in support for sending emails through the System.Net.Mail namespace.

Prerequisites

  • Visual Studio installed on your system
  • A basic knowledge of .Net and C#

Send an Email

We will be using Google's SMTP servers for this example, you will need to enable Less secure app access on your profile's Security page: https://myaccount.google.com/lesssecureapps

var smtpClient = new SmtpClient("smtp.gmail.com")
{
  Port = 587,
  Credentials = new NetworkCredential("[email protected]", "yourpassword"),
  EnableSsl = true,
};

smtpClient.Send("[email protected]", "[email protected]", "Sending Email with .Net", "Hello World!");

There's an overloaded Send-method that accepts a MailMessage object to help build the email message:

MailMessage message = new MailMessage("[email protected]", "[email protected]", "Sending Email with .Net", "Hello World!");
NetworkCredential netCred = new NetworkCredential("[email protected]","yourpassword");
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.Credentials = netCred;
smtpClient.Send(message);

Using Mailazy

Mailazy supports both SMTP and Email API. With SMTP, you can use either the SmtpClient class or the MailKit package. All you need to do is to replace the SMTP server, port, and credentials with those obtained from Mailazy.

using System.Net;
using System.Net.Mail;

class Example
{
static void Main(string[] args)
{
var mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("[email protected]", "sender");
mail.To.Add("[email protected]");

//set the content
mail.Subject = "Sending with Mailazy";

//set the plain text part
var plainView = AlternateView.CreateAlternateViewFromString("Hello World!", System.Text.Encoding.UTF8,
"text/plain");
//then set the html part
var htmlView = AlternateView.CreateAlternateViewFromString("<b>Hello World!</b>",
System.Text.Encoding.UTF8, "text/html");
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);

//send the message
var smtp = new SmtpClient("smtp.mailazy.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("<access_key>", "<access_secret>");
smtp.EnableSsl = true;
smtp.Send(mail);

}
}

A
Abhijeet Chattarjee

Email Design Specialist & Template Developer

 

A seasoned Product Marketing expert with extensive experience in product management and marketing strategies. Skilled in driving go-to-market plans, customer engagement, and cross-functional collaboration, they excel at aligning product development with market needs to deliver growth. Passionate about creating compelling product narratives, they combine data-driven insights with innovative solutions to boost brand visibility and customer satisfaction.

Related Articles

seo

6 Authentic Ways to Trace an IP Address from Email

Learn effective methods to trace IP addresses from emails for security verification, spam identification, and enhanced email management with practical step-by-step guidance.

By Abhijeet Chattarjee June 30, 2025 6 min read
Read full article
marketing

Demystifying Email Delivery: A Deep Dive into Email APIs, SMTP Servers, and Email Delivery Services

This blog post dives into the world of email APIs, SMTP servers, and email delivery services. Learn how they work together to get your emails into inboxes, not spam folders. Boost your email marketing results with this essential guide!

By Abhijeet Chattarjee June 30, 2025 7 min read
Read full article
content

21 SMTP Response Codes

The SMTP response code list can be used to help quickly determine the reason for email bounces or why you received an SMTP error when sending an email.

By Rahul Yadav June 30, 2025 6 min read
Read full article
digital

6 Tips for Defending Against Business Email Cybercriminal Attacks

Learn how to defend your firm from business email cybercriminal attacks, including how to spot and block them. Discover important tips with Mailazy now!

By Abhijeet Chattarjee June 30, 2025 6 min read
Read full article