How to send emails in Dotnet

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

Kunal Saini
Kunal Saini
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.

Free Sign UpSet up in 2 minutes . No Credit Card Required

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);
  }
}


Mailazy Docs

Integrate with Transactional email service in minutes

click here

Most Popular Tags

EngineeringSMTPShort ReadAPIsBest PracticesEmailEmail SecurityEmail TemplatesGolangHTTP



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.

Lets get started!

Kunal Saini

Kunal Saini

Kunal 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