How to send emails in Dotnet

How to send emails in .NET and .NET Core with the standard System.Net.Mail namespace using Gmail

Pranav Sharma
Pranav Sharma
August 23, 2021
1 min read

The Email API by developers, for developers

Integrate in minutes with our Email API or SMTP and deliver emails to customer's inbox instantly. Mailazy Email API built for developers that fits into any tech stack.


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


  • 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:

var smtpClient = new SmtpClient("")
  Port = 587,
  Credentials = new NetworkCredential("", "yourpassword"),
  EnableSsl = true,

smtpClient.Send("", "", "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("", "", "Sending Email with .Net", "Hello World!");
NetworkCredential netCred = new NetworkCredential("","yourpassword");
SmtpClient smtpClient = new SmtpClient("", 587);
smtpClient.EnableSsl = true;
smtpClient.Credentials = netCred;

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("", "sender");

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

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

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

Mailazy Docs

Integrate with Transactional email service in minutes

click here

Most Popular Tags

EngineeringSMTPShort ReadBest PracticesEmailAPIsEmail SecurityCommunicationEmail APIEmail Delivery

What is Mailazy?

Mailazy is a Transactional Email Platform specially built for developers which satisfies the requirement for use cases like Reset Password Emails, OTP Emails, Welcome Emails, and so on. The Mailazy platform helps you to send transactional emails seamlessly and track email deliverability. Mailazy enables your applications to send messages via a simple HTTP REST interface or via easy SMTP integration and abstracts away the complexities of sending transactional emails.

Visit website

Pranav Sharma

Pranav Sharma

Pranav Sharma is a Lead Software Development Engineer at Mailazy. He graduated in Computer Science and considers himself a learner of life. He loves to play cricket, football and computer games.

View Profile